Yoot
Yoot

Reputation: 657

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :

<p>{{ my_variable }}</p>

The thing is this variable may contain html tags, such as "<br />".

Twig seems to automatically call some htmlentities-like function when displaying variables.

Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :

Hello 
world !

rather than :

Hello<br />world !

Thanks

Upvotes: 12

Views: 19645

Answers (4)

Bevelopper
Bevelopper

Reputation: 75

If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Upvotes: 0

Mirage
Mirage

Reputation: 31548

Try using this

{% autoescape false %}{{ my_variable}}{% endautoescape %}

Upvotes: 2

Florian Klein
Florian Klein

Reputation: 8915

even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.

Upvotes: 1

dbrumann
dbrumann

Reputation: 17166

Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.

See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html

Upvotes: 23

Related Questions