blob: 234a28988a014d65166d8b3c55a9d0cc6e13b362 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``defined``
2===========
3
4``defined`` checks if a variable is defined in the current context. This is very
5useful if you use the ``strict_variables`` option:
6
7.. code-block:: twig
8
9 {# defined works with variable names #}
10 {% if foo is defined %}
11 ...
12 {% endif %}
13
14 {# and attributes on variables names #}
15 {% if foo.bar is defined %}
16 ...
17 {% endif %}
18
19 {% if foo['bar'] is defined %}
20 ...
21 {% endif %}
22
23When using the ``defined`` test on an expression that uses variables in some
24method calls, be sure that they are all defined first:
25
26.. code-block:: twig
27
28 {% if var is defined and foo.method(var) is defined %}
29 ...
30 {% endif %}