是否可以设置一个指令,即Twig模板中某个范围内的每个变量都将使用原始过滤器进行转义?
Ex。
{% 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 %}
**和/或**
是否有方法在控制器中指示变量将被呈现为原始
Ex。
// 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