在Twig三元运算符中使用引号

时间:2017-12-09 20:00:20

标签: php twig

在树枝三元运算符中使用时,如何防止引号被转义?我的愿望是<div id="my_id">my div11</div>(或<div id='my_id'>my div11</div>),但是,我看到的结果不同。

{% set id="my_id" %}
<div{{ id?" id='"~id~"'" }}>my div1</div>
<div{{ id?' id="'~id~'"' }}>my div2</div>
<div{{ id?" id='#{id}'" }}>my div3</div>
<div{{ id?" id=/'#{id}/'" }}>my div4</div>
<div{{ id?' id='~"'"|raw~id~"'"|raw }}>my div5</div>
<div{{ id?' id='~'"'|raw~id~"'"|raw }}>my div6</div>
<div{{ id?" id="~id }}>my div7</div>
<div{{ id?' id='~id }}>my div8</div>
<div{{ id?" id=#{id}" }}>my div9</div>

{% set id="" %}
<div{{ id?" id='"~id~"'" }}>my div1</div>
<div{{ id?' id="'~id~'"' }}>my div2</div>
<div{{ id?" id='#{id}'" }}>my div3</div>
<div{{ id?" id=/'#{id}/'" }}>my div4</div>
<div{{ id?' id='~"'"|raw~id~"'"|raw }}>my div5</div>
<div{{ id?' id='~'"'|raw~id~"'"|raw }}>my div6</div>
<div{{ id?" id="~id }}>my div7</div>
<div{{ id?' id='~id }}>my div8</div>
<div{{ id?" id=#{id}" }}>my div9</div>

输出:

<div id=&#039;my_id&#039;>my div1</div>
<div id=&quot;my_id&quot;>my div2</div>
<div id=&#039;my_id&#039;>my div3</div>
<div id=/&#039;my_id/&#039;>my div4</div>
<div id=&#039;my_id&#039;>my div5</div>
<div id=&quot;my_id&#039;>my div6</div>
<div id=my_id>my div7</div>
<div id=my_id>my div8</div>
<div id=my_id>my div9</div>

<div>my div1</div>
<div>my div2</div>
<div>my div3</div>
<div>my div4</div>
<div>my div5</div>
<div>my div6</div>
<div>my div7</div>
<div>my div8</div>
<div>my div9</div>

1 个答案:

答案 0 :(得分:1)

试试这个

  <div{{ (id?' id="'~id~'"')|raw }}>my div1</div>