From d5f4ec309b2be1593f654787f1abe0ea7d1c98b6 Mon Sep 17 00:00:00 2001 From: kimu Date: Thu, 23 May 2013 18:41:50 +0200 Subject: [PATCH] Added info about variable declarations inside a loop --- doc/tags/set.rst | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) 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) }} + + + -- 1.7.2.5