jinja: 2.9 regression when assigning a variable inside a loop
2.9:
>>> jinja2.Template('{% set a = -1 %}{% for x in range(5) %}[{{ a }}:{% set a = x %}{{ a }}] {% endfor %}{{ a }}').render()
u'[:0] [:1] [:2] [:3] [:4] -1'
2.8:
>>> jinja2.Template('{% set a = -1 %}{% for x in range(5) %}[{{ a }}:{% set a = x %}{{ a }}] {% endfor %}{{ a }}').render()
u'[-1:0] [0:1] [1:2] [2:3] [3:4] -1'
Originally reported on IRC:
A change in jinja2 scoping appears to affect me, and I’m unsure of the correct fix. Specifically the problem is the assignment of year here: https://github.com/kennethlove/alex-gaynor-blog-design/blob/551172/templates/archive.html#L13-L24
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 65 (48 by maintainers)
Commits related to this issue
- Implement consistent scoping for sets in loops While technically this applies to any scope and not just for loops it comes up most commonly in the context of for loops. This now defines the behavior... — committed to pallets/jinja by mitsuhiko 7 years ago
- Mention new set scoping behavior. Refs #641 — committed to pallets/jinja by mitsuhiko 7 years ago
- Pinning Jinja to <2.9 for now Variables defined in an outer scope can no longer be set from an inner scope (see pallets/jinja#641). Regardless of whether that is right or wrong, we can't control if p... — committed to OctoPrint/OctoPrint by foosel 7 years ago
- Pin-down Jinja to bypass variable scoping in for loop in 2.9. This cause side-by-side ellipsis items to not be detected when rendering pagination widget in Plumage: https://github.com/kdeldycke/pluma... — committed to kdeldycke/kevin-deldycke-blog by kdeldycke 7 years ago
- Add prevval/nextval to loop context closes #641 — committed to ThiefMaster/jinja by ThiefMaster 7 years ago
- Add previtem/nextitem to loop context closes #641 — committed to ThiefMaster/jinja by ThiefMaster 7 years ago
- Add previtem/nextitem to loop context related: #641 — committed to ThiefMaster/jinja by ThiefMaster 7 years ago
- Revert to Ninja 2.8 because of regression. See https://github.com/pallets/jinja/issues/641 Signed-off-by: Rob Caelers <rob.caelers@gmail.com> — committed to rcaelers/workrave by rcaelers 7 years ago
- Added missing issues for Steven and the Crystal Gems; updated ad script; changed some styles and got around that Jinja2 problem https://github.com/pallets/jinja/issues/641 — committed to sugrocks/website by mkody 7 years ago
- Add previtem/nextitem to loop context related: #641 — committed to ThiefMaster/jinja by ThiefMaster 7 years ago
changed_from_lastsounds good - at least functionality-wise, the name itself is a bit awkward IMO. Maybe justchangedwould be clear enough?I guess the first element would always be considered “changed” no matter what it is? If that behavior is not OK for someone they could always add a
not loop.firstcheck anyway.ok, so at least no risk of templates causing unexpected side-effects on objects passed to them.
still a bit wary of the things people might do…
Actually, I think a new block like
{% namespace ns %}would be better to define one than a callable - a variable namednamespacedoesn’t sound like something very unlikely to be passed to a template, and while it would probably simply prevent you from using the namespace feature in that template (just like shadowing a builtin in Python) it feels a bit dirty…Hi, I understand that there are a lot of “ugly” examples due to this, but could you please advise how to increment a variable on a for loop in jinja template in elegant/right way? Because my code has been broken too.
@pujan14
Although since
uniqueisn’t a built-in filter, you could always just add another filter that does the whole thing, since you’re already adding theuniquefilter.Filter syntax would not be possible here since filters are already possible on the iterable.
A possible syntax for this that wouldn’t look to bad could be this:
Especially since
withalready does scoping stuff in Jinja when used e.g. as{% with %}. OTOH, here it would be the opposite since with blocks open a new scope, not make outer scope vars accessible…Not sure though if that’s a feature Jinja needs or not.