dp610807 2016-08-16 16:06
浏览 282
已采纳

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

  • 写回答

3条回答 默认 最新

  • dongni9825 2016-08-16 16:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?