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 转义


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01