Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``defined`` |
| 2 | =========== |
| 3 | |
| 4 | ``defined`` checks if a variable is defined in the current context. This is very |
| 5 | useful 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 | |
| 23 | When using the ``defined`` test on an expression that uses variables in some |
| 24 | method 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 %} |