手动渲染 Twig 字符串时禁用 HTML 转义 [英] Disable HTML escaping when manually rendering a Twig string

查看:35
本文介绍了手动渲染 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));

输出:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆