From: kimu Date: Thu, 23 May 2013 16:41:50 +0000 (+0200) Subject: Added info about variable declarations inside a loop X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=d5f4ec309b2be1593f654787f1abe0ea7d1c98b6;p=web%2Fkonrad%2Ftwig.git Added info about variable declarations inside a loop --- diff --git a/doc/tags/set.rst b/doc/tags/set.rst index ac5ce53..2ddff85 100644 --- a/doc/tags/set.rst +++ b/doc/tags/set.rst @@ -52,3 +52,18 @@ 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 + + {% set foo = "" %} + {% for item in list %} + {% set foo = item %} + {% endfor %} + {{ dump(foo) }} + + +