Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable HTML escaping when manually rendering a Twig string

Tags:

html

twig

symfony

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.

like image 933
mattalxndr Avatar asked Dec 21 '12 12:12

mattalxndr


1 Answers

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

$twig = new \Twig_Environment(new \Twig_Loader_String, array(
    'autoescape' => false
));
like image 154
mattalxndr Avatar answered Sep 17 '22 12:09

mattalxndr