手动呈现Twig字符串时禁用HTML转义 [英] Disable HTML escaping when manually rendering a Twig string
本文介绍了手动呈现Twig字符串时禁用HTML转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码将一个字符串呈现为HTML输出。我怎样才能阻止它逃离HTML的文本?
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"
据我所知,将'{{who}} bar'
更改为' {{who | raw}} 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.
推荐答案
我通过Twig代码挖掘,发现这工作正常:
I dug through the Twig code and found that this works fine:
$twig = new \Twig_Environment(new \Twig_Loader_String, array(
'autoescape' => false
));
这篇关于手动呈现Twig字符串时禁用HTML转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文