在if速记中转义html的细枝?
我有一个模板如下:在if速记中转义html的细枝?,html,twig,Html,Twig,我有一个模板如下: <div class="reply_text"><div class="wall_reply_text"> {{ reply.comment_text is empty ? '<div class="has-text-grey-light">This comment is empty.</div>' : reply.comment_text }} </div></div> {{reply.co
<div class="reply_text"><div class="wall_reply_text">
{{ reply.comment_text is empty ? '<div class="has-text-grey-light">This comment is empty.</div>' : reply.comment_text }}
</div></div>
{{reply.comment_text为空?'此评论为空。':reply.comment_text}
不是显示此注释为空。
使用类的文本为灰色
,而是逐字显示此未转义文本:此注释为空。
我相信以前我已经做过类似的事情,并且它的输出是正确的。这里怎么了?试试过滤器
{{var|raw}{{var不会被转义}
{{(对吗?'Hello 1':'Hello 2')|raw}
raw筛选器将该值标记为“安全”,这意味着在启用自动转义的环境中,如果raw是应用于该变量的最后一个筛选器,则不会转义该变量:
<div class="reply_text">
<div class="wall_reply_text">
{{ (reply.comment_text is empty ? '<div class="has-text-grey-light">This comment is empty.</div>' : reply.comment_text) | raw }}
</div>
</div>
{{(reply.comment_text为空?'此评论为空。':reply.comment_text)| raw}
另一种方法是在不需要使用原始过滤器的情况下使用if-else语句
{% if reply.comment_text is empty %}
<div class="has-text-grey-light">This comment is empty.</div>
{% else %}
{{ reply.comment_text }} {# if required {{ reply.comment_text | raw }} #}
{% endif %}
{%如果reply.comment\文本为空%}
此评论为空。
{%else%}
{{reply.comment_text}}{{#如果需要{{{reply.comment_text}raw}}
{%endif%}
谢谢,它成功了。但我可以发誓,我已经做了,没有生的,它的工作。原因可能是什么?除非我看到代码,否则无法判断,但如果使用if-else语句,则可以这样做。我记得!我做了同样的速记,但对于有条件地选择div标记内的类,标记不在条件内。但我现在明白了,所以你不能用速记法。
{% if reply.comment_text is empty %}
<div class="has-text-grey-light">This comment is empty.</div>
{% else %}
{{ reply.comment_text }} {# if required {{ reply.comment_text | raw }} #}
{% endif %}