How to display HTML tags stored in a database using Symfony 2 and Twig

I managed to create a page that will dynamically display data from the database. However, this code is stored in the database:

<p>This is the content for the radio <a href="#">page</a>.</p> 

It is displayed as follows:

 <p>This is the content for the radio <a href="#">page</a>.</p> 

HTML tags are not displayed. I understand that Symfony (for security purposes) provides HTML code like this for security reasons. I want Symfony (obviously) to display these HTML tags. How can I achieve this for this purpose only, so that Symfony will still sanitize any HTML tag that is stored in the database elsewhere on the site?

For your information, this is the code that I use to pull data from a database:

  public function mainpageAction($slug) { $content = $this->getDoctrine() ->getRepository('SiteMainBundle:Content') ->find($slug); if (!$content) { throw $this->createNotFoundException('No product found for slug '.$slug); } return $this->render('SiteMainBundle:Default:page.html.twig', array('content' => $content, 'slug' => $slug)); } 

Also, so that I can learn more about Symfony, is rendering HTML tags the only PHP job or can it be displayed correctly using Twig?

Many thanks!

+6
source share
1 answer

If you want to do this, you need to use the raw filter in the branch template. As described in the branch documentation, an unprocessed filter marks the value as safe, which means that in an environment with automatic shielding, this variable will not be shielded if the original filter is applied to the last filter.

In your case, this is: {{ content | raw }} {{ content | raw }}

+22
source

Source: https://habr.com/ru/post/895405/


All Articles