Added info about variable declarations inside a loop

This commit is contained in:
kimu
2013-05-23 18:41:50 +02:00
committed by Fabien Potencier
parent ba88c75557
commit d5f4ec309b
+15
View File
@@ -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 If you enable automatic output escaping, Twig will only consider the
content to be safe when capturing chunks of text. 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) }}