问题
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