Variables inside loops (not outside...)

Hi guys,

I’m struggling to understand the variable assignment and scoping logic. It seems that the assignements are being scoped inside the “if” statement but I just can’t believe this is intended behaviour… What am I missing here?

Thanks,
-Miguel

Believe it or not, it is the intended behavior. If you want a variable’s scope to exist outside a block you need to use a namespace. See “Scoping Behavior” here:

https://jinja.palletsprojects.com/en/master/templates/#assignments

Thanks for the quick and to the point answer :smiley:

I just had to replace the initial assignement with “ns = namespace(first=true)”, and then reference the variable with “ns.first” and it worked like a charm!

1 Like