[英]Global raw escaping in Twig
是否可以设置一个指令,即某个 scope 中的 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 中指示变量将呈现为原始
前任。
// 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.