Treating "for" loops as a separate scope does not behave reasonably
See original GitHub issueThis code:
{% set lastDate = '' %}
{% for item in items %}
{{ renderEvent(event, item, lastDate) }}
{% set lastDate = item.date %}
{% endfor %}
Does not behave as a programmer accustomed to [fill in pretty much any language here] would expect.
The renderEvent macro receives ‘’ on every iteration, because it sees the lastDate variable declared in the outer scope.
The second set statement creates a separate lastDate variable in an inner scope but it is too late for renderEvent to see it.
If we get rid of the first “set” entirely, the code starts working; renderEvent passes undefined on the first pass, and the value of the second “set” on each pass thereafter.
This works, but it requires us to lean heavily on nunjuck’s tolerance for passing undefined variables, and if we needed to initialize to something other than “nothing” we’d be out of luck.
IMHO a for loop should never create a new variable scope in a language that has no distinction between declaring a variable (“var foo”) and updating it.
Issue Analytics
- State:
- Created 10 years ago
- Comments:38 (15 by maintainers)
Top GitHub Comments
I still don’t think creating new scopes inside “for” loops makes sense in a language without explicit declarations. It’s just not possible to correctly infer what is supposed to happen.
On Tue, Dec 9, 2014 at 3:56 PM, Felipe Nascimento de Moura < notifications@github.com> wrote:
*THOMAS BOUTELL, *DEV & OPS P’UNK AVENUE | (215) 755-1330 | punkave.com
In the case we are facing here…
In this case we need to, using the loop, identify the selected thumb to show it afterwards in a bigger size. But it does not work, because the
highlight
variable is gone! I know that theset
token creates the new variable inside thefor
scope, but how can we get that to work? I see the issue is closed, but couldn’t figure out if this is a bug, or if there is a way to do it, but it is not documented anywhere!