symfony2 twig whitelist html tags symfony2 twig whitelist html tags symfony symfony

symfony2 twig whitelist html tags


Actually, you can use native PHP function strip_tags by following:

{{ var|striptags('<br>')|raw }}

you can allow multiple tags with following code:

{{ var|striptags('<br><p>')|raw }}


You can do like that :

{{ text | striptags('<p><b><br') | raw }}

For instance,

<br>

won't escape

<br> and <br />

and

<p>

won't escape

<p> and </p>

etc.


Initially I thought it should be possible to write custom escaper strategies so you could do something like this:

{{ var|escape('html-custom') }}

Unfortunately it's not the case. Only available strategies are html and js. They're hard coded in the twig_escape_filter() function defined in a Twig_Extension_Core class file.

It seems that your only option is to write custom estension with a new filter:

{{ var|raw|customescape }}

Here's an example of custom twig extension and how to register it in Symfony: Symfony2 Twig extension