k0p*_*kus 4 php html-encode html-entities symfony twig
我想创建一个包含 html 编码块的 xml 输出。
这是我的树枝片段:
<rawXml>
<message>
{% autoescape 'html' %}
<ThisShouldBeEscaped>
<ButItIsnt>Dang</ButItIsnt>
</ThisShouldBeEscaped>
{% endautoescape %}
</message>
</rawXml>
Run Code Online (Sandbox Code Playgroud)
在渲染时,我希望以这种方式对消息内容进行 html 编码:
<ThisShouldBeEscaped>
<ButItIsnt>Dang</ButItIsnt>
</ThisShouldBeEscaped>
Run Code Online (Sandbox Code Playgroud)
但我得到了完整的原始 XML 响应:
<rawXml>
<message>
<ThisShouldBeEscaped>
<ButItIsnt>Dang</ButItIsnt>
</ThisShouldBeEscaped>
</message>
</rawXml>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
默认情况下,Twig 不会转义模板标记。如果您希望以这种方式转义 HTML,请先将其设置为变量,然后再将autoescape
其设置为变量,或者使用常规的escape
:
<rawXml>
<message>
{% set myHtml %}
<ThisShouldBeEscaped>
<ButItIsnt>Dang</ButItIsnt>
</ThisShouldBeEscaped>
{% endset %}
{% autoescape 'html' %}
{{ myHtml }}
{% endautoescape %}
<!-- or -->
{{ myHtml|escape }}
</message>
</rawXml>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1106 次 |
最近记录: |