Display a string that contains HTML in twig templa

2019-01-10 09:49发布

问题:

How can I display a string that contains HTML tags in twig template?

My PHP variable contains this html and text:

$word = '<b> a word </b>';

When I do this in my twig template:

{{ word }}

I get this:

&lt;b&gt; a word &lt;b&gt;

I want this instead:

<b> a word </b>

Is it possible to get this easily?

回答1:

Use raw keyword, http://twig.sensiolabs.org/doc/api.html#escaper-extension

{{ word | raw }}


回答2:

You can also use:

{{ word|striptags('<b>')|raw }}

so that only <b> tag will be allowed.



回答3:

{{ word|striptags('<b>,<a>,<pre>')|raw }}

if you want to allow multiple tags