From 88dd4ac5797a4ffe702a5eb5a0e5f5f5bea187bf Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 30 Oct 2011 14:47:32 +0100 Subject: [PATCH] added notes in the doc about how the defined test and the default filter work --- doc/filters/default.rst | 9 +++++++++ doc/tests/defined.rst | 9 +++++++++ 2 files changed, 18 insertions(+), 0 deletions(-) diff --git a/doc/filters/default.rst b/doc/filters/default.rst index 3c1bc5b..4055ead 100644 --- a/doc/filters/default.rst +++ b/doc/filters/default.rst @@ -9,10 +9,19 @@ undefined or empty, otherwise the value of the variable: {{ var|default('var is not defined') }} {{ var.foo|default('foo item on var is not defined') }} + {{ var['foo']|default('foo item on var is not defined') }} {{ ''|default('passed var is empty') }} +When using the ``default`` filter on an expression that uses variables in some +method calls, be sure to use the ``default`` filter whenever a variable can be +undefined: + +.. code-block:: jinja + + {{ var.method(foo|default('foo'))|default('foo') }} + .. note:: Read the documentation for the :doc:`defined<../tests/defined>` and diff --git a/doc/tests/defined.rst b/doc/tests/defined.rst index a6164e5..702ce72 100644 --- a/doc/tests/defined.rst +++ b/doc/tests/defined.rst @@ -19,3 +19,12 @@ useful if you use the ``strict_variables`` option: {% if foo['bar'] is defined %} ... {% endif %} + +When using the ``defined`` test on an expression that uses variables in some +method calls, be sure that they are all defined first: + +.. code-block:: jinja + + {% if var is defined and foo.method(var) is defined %} + ... + {% endif %} -- 1.7.2.5