简体   繁体   English

全局原始 escaping 在 Twig

[英]Global raw escaping in Twig

Is it possible to set a directive that every variable in a Twig template within a certain scope will be escaped with the raw filter ?是否可以设置一个指令,即某个 scope 中的 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 %}

Instead of having to explicitly write而不必显式地写

    {{foo|raw}} 
    {{bar|raw}}
    {{baz|raw}}

It would be great if this was inherited by child templates..如果它被子模板继承那就太好了..

{% 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 %}

** AND/OR ** ** 和/或 **

Is there a way to indicate in the controller that a variable is to be rendered as raw有没有办法在 controller 中指示变量将呈现为原始

Ex.前任。

// Controller

$data['foo'] = renderAsRaw($foo);

return new Response($this->renderView('template.html.twig', $data));

// Template

{{foo}} // will be rendered as raw

I tried using the autoescape but this does not work as I have described above我尝试使用自动转义,但这不起作用,正如我上面描述的那样

{% autoescape %}
    {{foo}} // this does NOT render as raw
{% endautoescape %}

All your templates by default use autoescaping.默认情况下,您的所有模板都使用自动转义。

You can disable autoescape for part of your template by adding false in autoescape block declaration:您可以通过在autoescape块声明中添加false来禁用部分模板的autoescape

{% autoescape false %}
    {{ rawVar }}
{% endautoescape %}

If you need to disable autoescaping in all your templates you can set global parameter in config.yml:如果您需要在所有模板中禁用自动转义,您可以在 config.yml 中设置全局参数:

twig:
    autoescape: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM