Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 twig whitelist html tags

I pass a variable to my twig template in Symfony2, this variable may contain <br /> html tags, I have tried to create an extension (function), but the variable still gets escaped.

How can I output a twig variable that allows the <br /> tag? Is there a simple solution to just allow a whitelist of allowed tags in certain templates?

I've searched about twig sandboxes, but I'm not sure if that is my solution.

edit: I still want the variable to be escaped, but to allow exclusively the <br /> tag.

like image 408
jonv1 Avatar asked Nov 03 '11 19:11

jonv1


1 Answers

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 }}
like image 107
Artem L Avatar answered Sep 28 '22 06:09

Artem L