How to display string that contains HTML in twig template?

04/04/2020 10:00:01

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?

Answer #1 (75 Votes)

05/09/2012 00:12:54

You can also use:

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

so that only <b> tag will be allowed.


Answer #2 (29 Votes)

06/09/2016 22:02:23
{{ word|striptags('<b>,<a>,<pre>')|raw }}

if you want to allow multiple tags

Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.