2016-08-16 16:06

Twig不呈现html标签,| raw不起作用


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('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">

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 :(

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongni9825 dongni9825 5年前

    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.

    点赞 评论 复制链接分享
  • duanchi3109 duanchi3109 5年前

    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.

    点赞 评论 复制链接分享
  • dopgv00024 dopgv00024 5年前

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

       entity_encoding: "raw"

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

    点赞 评论 复制链接分享