Disable HTML escaping when manually rendering a Twig string

时光总嘲笑我的痴心妄想 提交于 2020-01-12 13:47:14

问题


I have the following code that renders a string into HTML output. How can I stop it from escaping the text for HTML?

$template = '{{ who }} bar';
$params = array('who' => "Foo's");

$twig = new \Twig_Environment(new \Twig_Loader_String);
var_dump($twig->render($template, $params));

Outputs:

string(14) "Foo's bar"

How can I make it output this instead?

string(14) "Foo's bar"

I understand that changing '{{ who }} bar' to '{{ who|raw }} bar' will fix the problem, but I want to solve this at the rendering stage. I do not want to change all of the templates.


回答1:


I dug through the Twig code and found that this works fine:

$twig = new \Twig_Environment(new \Twig_Loader_String, array(
    'autoescape' => false
));


来源:https://stackoverflow.com/questions/13990249/disable-html-escaping-when-manually-rendering-a-twig-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!