我正在使用 Twig 显示在我的数据库中检索到的一些变量:
<p>{{ my_variable }}</p>
问题是这个变量可能包含html标签,比如“ <br />”。
Twig 在显示变量时似乎会自动调用一些类似 htmlentities 的函数。
有什么方法可以禁用它,以便当我显示包含“ Hello<br />world !”的变量时,我得到:
Hello
world !
而不是 :
Hello<br />world !
谢谢
我正在使用 Twig 显示在我的数据库中检索到的一些变量:
<p>{{ my_variable }}</p>
问题是这个变量可能包含html标签,比如“ <br />”。
Twig 在显示变量时似乎会自动调用一些类似 htmlentities 的函数。
有什么方法可以禁用它,以便当我显示包含“ Hello<br />world !”的变量时,我得到:
Hello
world !
而不是 :
Hello<br />world !
谢谢
用于{{ my_variable|raw }}防止my_variable被自动转义。
请参阅 Twig 文档:http ://twig.sensiolabs.org/doc/filters/raw.html
尝试使用这个
{% autoescape false %}{{ my_variable}}{% endautoescape %}
更好的是:{{ '<br />|raw('html') }}避免逃避其他明智的事情。
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>