misterlewy1
misterlewy1

Reputation: 65

Twig doesn't render html tags, |raw doesn't work

I've got a simple blog application written in Symfony3, I'm using TinyMCE as a my text editor. I'm storing data in database, where example record looks like this:

<p>Lorem ipsum</p>

I get articles from DB using this method:

  /**
  * @Template
  */

  public function adminAction()
  {
  $repository = $this->getDoctrine()->getRepository('BlogBundle:Post');

  $lastPosts = $repository->findBy(
  array(),
  array('id' => 'DESC'), 4
  );

  return array(
    'last_posts' => $lastPosts,
  );
  }

And handle returned values in twig like this (It's just a part of code):

<div class="panel-body">
  {{post.text|raw}}
</div>

And the output looks like this:

<p>Lorem ipsum</p>

What should I do to get rid off html tags and just let them render. I've tried to clear cache, same result :(

Upvotes: 1

Views: 8481

Answers (3)

misterlewy1
misterlewy1

Reputation: 65

Ok, so the problem was with configuration of my tinyMCE editor, I've used

<script>
  tinymce.init({
   ...
   entity_encoding: "raw"
  });
 </script>

So now my database stores HTML tags instead of escaped HTML. Thanks @Terenoth for explain

Upvotes: 1

Terenoth
Terenoth

Reputation: 2598

I don't know why you have &lt;p&gt;Lorem ipsum&lt;/p&gt; as HTML content in your database, since it is NOT HTML, it is escaped HTML. Using raw unescapes the content, so your escaped HTML becomes... wait for it... HTML. And since twig escapes automatically HTML, that's why tags are displayed on your page.

What you could try as a quick fix is {{post.text|raw|raw}} to unescape the unescaped HTML. But honestly, what I would recommend for the sanity of your application is to unescape the HTML directly in your database. You SHOULD have <p>Lorem ipsum</p> in your database, then using {{post.text|raw}} should work perfectly.

Upvotes: 3

Koalabaerchen
Koalabaerchen

Reputation: 575

You need to unescape the output. If you can't do a htmlspecialchars_decode inside your php, you could try it with a twig extension decoding it.

Upvotes: 0

Related Questions