在树枝三元运算符中使用时,如何防止引号被转义?我的愿望是<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='my_id'>my div1</div>
<div id="my_id">my div2</div>
<div id='my_id'>my div3</div>
<div id=/'my_id/'>my div4</div>
<div id='my_id'>my div5</div>
<div id="my_id'>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>
答案 0 :(得分:1)
试试这个
<div{{ (id?' id="'~id~'"')|raw }}>my div1</div>