defined.rst 694B

12345678910111213141516171819202122232425262728293031
  1. ``defined``
  2. ===========
  3. ``defined`` checks if a variable is defined in the current context. This is very
  4. useful if you use the ``strict_variables`` option:
  5. .. code-block:: jinja
  6. {# defined works with variable names #}
  7. {% if foo is defined %}
  8. ...
  9. {% endif %}
  10. {# and attributes on variables names #}
  11. {% if foo.bar is defined %}
  12. ...
  13. {% endif %}
  14. {% if foo['bar'] is defined %}
  15. ...
  16. {% endif %}
  17. When using the ``defined`` test on an expression that uses variables in some
  18. method calls, be sure that they are all defined first:
  19. .. code-block:: jinja
  20. {% if var is defined and foo.method(var) is defined %}
  21. ...
  22. {% endif %}