Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of Twig "include with only"?

Tags:

twig

symfony

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.

like image 634
gremo Avatar asked Mar 30 '12 00:03

gremo


People also ask

What is Twig used for?

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.

What are Twig filters?

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.

What is a Twig in JavaScript?

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.


1 Answers

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.

like image 132
Elnur Abdurrakhimov Avatar answered Sep 23 '22 18:09

Elnur Abdurrakhimov