Giulia Chiola
Posted on • Originally published atgiuliachiola.dev
Nunjucks scoped variable declarations
We have to pay attention where we set Nunjucks variables because they arescoped
{%setanimals=['🐱','🐶','🐺']%}{%foriteminanimals%}{%setanimal=item%}{%endfor%}{{animal}}{# animal -> ERROR #}{# animal declared INSIDE the loop is NOT available #}
{%setanimals=['🐱','🐶','🐺']%}{# note this declaration #}{%setanimal=''%}{%foriteminanimals%}{%setanimal=item%}{%endfor%}{{animal}}{# animal declared OUTSIDE the loop is available #}{# animal -> 🐺 (last array item) #}
📚 More info
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse