手动渲染 Twig 字符串时禁用 HTML 转义字符串、Twig、HTML

2023-09-07 00:24:37 作者:落花满天飞

我有以下代码将字符串呈现为 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));

输出:

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
));
 
精彩推荐
图片推荐