From b081e3f1ed64b8246bfcadef3e66233ab2c0ffd9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 25 May 2013 17:56:39 +0200 Subject: [PATCH] tweaked previous merge (refs #1103) --- doc/tags/set.rst | 33 +++++++++++++++++++++------------ 1 files changed, 21 insertions(+), 12 deletions(-) diff --git a/doc/tags/set.rst b/doc/tags/set.rst index 2ddff85..3eba239 100644 --- a/doc/tags/set.rst +++ b/doc/tags/set.rst @@ -52,18 +52,27 @@ The ``set`` tag can also be used to 'capture' chunks of text: If you enable automatic output escaping, Twig will only consider the content to be safe when capturing chunks of text. - -Note that differently from PHP in Twig loops are scoped, therefore a variable -declared inside a for-in loop isn't accessibile outside the loop itself. -The variable must be declared before the loop to be accessibile elsewhere. -.. code-block:: jinja +.. note:: + + Note that loops are scoped in Twig; therefore a variable declared inside a + ``for`` loop is not accessible outside the loop itself: + + .. code-block:: jinja + + {% for item in list %} + {% set foo = item %} + {% endfor %} + + {# foo is NOT available #} + + If you want to access the variable, just declare it before the loop: + + .. code-block:: jinja - {% set foo = "" %} - {% for item in list %} - {% set foo = item %} - {% endfor %} - {{ dump(foo) }} - + {% set foo = "" %} + {% for item in list %} + {% set foo = item %} + {% endfor %} - + {# foo is available #} -- 1.7.2.5