php 如何在树枝模板中显示包含 HTML 的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8355123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display string that contains HTML in twig template?
提问by Gildas Ross
How can I display a string that contains HTML tags in twig template?
如何在树枝模板中显示包含 HTML 标签的字符串?
My PHP variable contains this html and text:
我的 PHP 变量包含此 html 和文本:
$word = '<b> a word </b>';
When I do this in my twig template:
当我在树枝模板中执行此操作时:
{{ word }}
I get this:
我明白了:
<b> a word <b>
I want this instead:
我想要这个:
<b> a word </b>
Is it possible to get this easily?
有没有可能很容易得到这个?
回答by Aurimas Li?kus
Use raw keyword, http://twig.sensiolabs.org/doc/api.html#escaper-extension
使用原始关键字,http://twig.sensiolabs.org/doc/api.html#escaper-extension
{{ word | raw }}
回答by Shimon S
You can also use:
您还可以使用:
{{ word|striptags('<b>')|raw }}
so that only <b>
tag will be allowed.
这样只<b>
允许标记。
回答by musicjerm
{{ word|striptags('<b>,<a>,<pre>')|raw }}
if you want to allow multiple tags
如果你想允许多个标签