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.
Twig is a modern template engine for PHP This allows Twig to be used as a template language for applications where users may modify the template design. Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.
Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.
Twig.js is a pure JavaScript implementation of the Twig PHP templating language (https://twig.symfony.com/) The goal is to provide a library that is compatible with both browsers and server side JavaScript environments such as node.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With