Convert string to HTML in Symfony


I'm having problems rendering a string (string) in HTML tags. This happens to me when using a form, which after previewing the data, but the problem is that it does not interpret the HTML tags, it just shows them to me.

The variable happened to twig in this way:

return $this->render('registro/index.html.twig', array(
            'articulo' => $producto->getArticleHtml(),

$producto is not more than the instance of an entity that I have previously declared, to then be able to upload to the database.

I visualize it this way in twig:

<p>{{ articulo }}</p>

Am I forgetting something? It is the first time I work with Symfony and this with pure PHP (without using any framework) worked for me without any problem.

Example of how it is displayed, with tags included:

<b>Esto es una prueba</b>
asked by JetLagFox 04.09.2018 в 01:04

1 answer


If you use template Twig the output escape is activated by default. If you want to disable it you can do it with raw , as the documentation says :


If you are using Twig templates, then the exit escape is   activated by default. This means that you are protected immediately   against the involuntary consequences of the code sent by the   user. By default, the exit escape assumes that   Content is leaking for HTML output.


In some cases, you should disable exit escaping when   represent a variable that is trustworthy and that contains marks that   You must not escape. Suppose that administrative users   They can write articles that contain HTML code. By default, Twig   will escape from the body of the article.


To render it normally, add the filter without format:

{{ article.body|raw }}

You can also deactivate the exit escape within a {% block%} area or for a full template. For more information,   see Exit Exit in the Twig documentation.

Following these guidelines, your code should work if you do this:

<p>{{ articulo | raw }}</p>
answered by 04.09.2018 / 02:17