gremo
gremo

Reputation: 48899

What's the purpose of 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:

Included templates have access to the variables of the active context. You can disable access to the context by appending the only keyword.

Upvotes: 17

Views: 8839

Answers (1)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44831

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.

Upvotes: 29

Related Questions