question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Treating "for" loops as a separate scope does not behave reasonably

See original GitHub issue

This 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:closed
  • Created 10 years ago
  • Comments:38 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
boutellcommented, Dec 9, 2014

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:

We tried that, we too thought what would be the behaviour, but this did not work, either! When the {% set highlight = image.url %} happens inside the loop, it ignores the “highlight” variable from outside! And it was “killed” in the end of each loop, becoming “” after the endfor statement! The version we are using here is 1.0.4.

— Reply to this email directly or view it on GitHub https://github.com/mozilla/nunjucks/issues/166#issuecomment-66356099.

*THOMAS BOUTELL, *DEV & OPS P’UNK AVENUE | (215) 755-1330 | punkave.com

1reaction
felipenmouracommented, Dec 4, 2014

In the case we are facing here…

{% for image in images %}
    {% if thumbs.selected == image.id %}
        {% set highlight = image.url %}
    {% endif %}
    <img src="image.thumbUrl" />
{% endfor %}
<img src="{{ highlight }}" />

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 the set token creates the new variable inside the for 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

The scope of index variables in Python's for loops
The for-loop makes assignments to the variables(s) in the target list. [...] Names in the target list are not deleted when the loop...
Read more >
New scope solution - Internals & Design
I have an alternate, non-breaking solution to the scope issue previously discussed on discourse and github. The new potential solution is the following...
Read more >
Scope of loops · JuliaNotes.jl
Scope behavior of loops · Ideally one would like that a loop like · There is no problem in writing such a loop...
Read more >
Explanation of `let` and block scoping with for loops
In short, when using let , the closure is at the loop body and the variable is different each time, so it must...
Read more >
Javascript Prototype & Scope Chains: What You Need to ...
Now newer version of V8 does not have such a limitation in the optimizing compiler and hoists lookup out of the inner loop,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found