Twig“仅包含"的目的是什么? [英] What's the purpose of Twig "include with only"?
问题描述
在 Twig 中 include with only
的真正目的是什么:
What's really the purpose of include with only
in Twig:
{# only the foo variable will be accessible #}
{% include 'child.html.twig' with {'foo': 'bar'} only %}
也许有一些性能好处?或者只是为了避免在包含的模板中覆盖变量?作为文档:
Maybe some performance benefits? Or just only for avoid overriding variables in the included template? As documentation:
包含的模板可以访问活动上下文的变量.您可以通过附加 only 关键字来禁用对上下文的访问.
推荐答案
当您包含模板时,它可以访问包含模板中的所有可用变量.如果出于某种原因您不希望那样,请使用 only
关键字.
When you include a template, it has access to all the variables available in the including template. If for some reason you don't want that, use the only
keyword.
我没有遇到过需要它的情况,但可能有性能以外的原因.例如,您可以使用它来避免某些情况下的命名冲突.
I haven't been in a situation when I needed that, but there might be reasons other than performance. For example, you could use it to avoid naming collisions in some scenarios.
这篇关于Twig“仅包含"的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!