Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Giulia Chiola
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 #}
Enter fullscreen modeExit fullscreen mode
{%setanimals=['🐱','🐶','🐺']%}{# note this declaration #}{%setanimal=''%}{%foriteminanimals%}{%setanimal=item%}{%endfor%}{{animal}}{# animal declared OUTSIDE the loop is available #}{# animal -> 🐺 (last array item) #}
Enter fullscreen modeExit fullscreen mode

📚 More info

Twig docs - set variables

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-end Developer. 👾Media engineer. ⚡Facing life one bug at a time. 🔥 Easily bribed with chocolate. 🍫 she/her.
  • Location
    Val di Susa, Italia
  • Education
    Media Engineer, Politecnico di Torino
  • Work
    Front-end web developer
  • Joined

More fromGiulia Chiola

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp