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));
输出:
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 转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:手动渲染 Twig 字符串时禁用 HTML 转义
基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
