php - Twig 中的全球原始逃逸
问题描述
是否可以设置一个指令,使特定范围内的 Twig 模板中的每个变量都将使用原始过滤器进行转义?
前任。
{% setAllRaw %}
{{foo}} // this will be rendered as if foo|raw
{{bar}} // this will be rendered as if bar|raw
{{baz}} // this will be rendered as if baz|raw
{% endSetAllRaw %}
而不必显式地写
{{foo|raw}}
{{bar|raw}}
{{baz|raw}}
如果它被子模板继承那就太好了..
{% setAllRaw %}
{{foo}} // this will be rendered as if foo|raw
{% include 'component.twig' %} // every variable in this template will also be rendered as raw
{% endSetAllRaw %}
** 和/或 **
有没有办法在控制器中指示变量将呈现为原始
前任。
// Controller
$data['foo'] = renderAsRaw($foo);
return new Response($this->renderView('template.html.twig', $data));
// Template
{{foo}} // will be rendered as raw
我尝试使用自动转义,但这不起作用,正如我上面描述的那样
{% autoescape %}
{{foo}} // this does NOT render as raw
{% endautoescape %}
解决方案
默认情况下,您的所有模板都使用自动转义。
您可以通过添加块声明来禁用autoescape
部分模板:false
autoescape
{% autoescape false %}
{{ rawVar }}
{% endautoescape %}
如果您需要在所有模板中禁用自动转义,您可以在 config.yml 中设置全局参数:
twig:
autoescape: false
推荐阅读
- python - 有人可以解释一下 np.less_equal.outer(range(1,18),range(1,13))
- c++ - 如何使用 libtins 捕获 HTTPS 数据包?
- spring - 使用带有 Spring Security 的 Spring Boot 启用 CSRF 相关问题时,如何修复它?
- python - 如何在 PyOpenGL 中旋转某个对象(四边形)?
- javascript - How to create a new class instance from within an instance?
- facebook-graph-api - Where to get product-catalog-id
- sql - 如何将特定行值转换为配置单元中的列标题
- javascript - Canvas, how to set line dash of line?
- node.js - 服务| -v | ubuntu 终端中的任何命令都不起作用,而在 intellij 的本地终端中它正在工作
- gitlab - GitLab API get commit count of a branch