如何使用Symfony 2和Twig显示保存在数据库中的HTML标记

mic*_*jnr 6 database symfony

我已经设法构建了一个动态显示数据库数据的页面.但是,此代码保存在数据库中:

<p>This is the content for the radio <a href="#">page</a>.</p>
Run Code Online (Sandbox Code Playgroud)

显示如下:

<p>This is the content for the radio <a href="#">page</a>.</p>
Run Code Online (Sandbox Code Playgroud)

不呈现HTML标记.我知道Symfony(出于安全目的)出于安全原因呈现这样的任何HTML代码.我希望Symfony(显然)能够呈现这些HTML标记.我如何才能仅为此目的实现此目的,以便Symfony仍然清理保存到站点上其他位置的数据库的任何HTML标记?

对于您的信息,这是我用来从数据库中提取数据的代码:

    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));
}
Run Code Online (Sandbox Code Playgroud)

另外,我可以了解更多关于Symfony的信息,HTML标签的渲染只是PHP的唯一工作,还是可以使用Twig正确渲染?

非常感谢!

ege*_*oen 23

如果要这样做,则需要raw在树枝模板中使用过滤器.与twig文档中描述的一样,原始过滤器将值标记为安全,这意味着在启用了自动转义的环境中,如果raw是应用于它的最后一个过滤器,则此变量将不会被转义.

在你的情况下,它是: {{ content | raw }}