Search code examples
loopsnunjucks

Loop index in included nunjucks file


So I came across a peculiar thing in nunjucks that I really need a fix for.

Imagine I have a loop in i.e. index.nj

{% for item in items %}
    {% include "/file2.nj" %}
{% endfor %}

now in file2.nj I wish to address the loop index as follows:

{{ loop.index }}

This doesn't work for some reason but the weird part is that if I first add a call to loop.index in the index.nj file, it does.

i.e.

{% for item in items %}
    {{loop.index}}
    {% include "/file2.nj" %}
{% endfor %}

In the example above, file2.nj can call loop.index itself. Why does it not know loop.index first but when I call it upfront (this can also be in an if that does nothing, I tested), the file does have access to it?

I would be overjoyed if anyone knows how to fix this.


Solution

  • This is a bug, resolved in this pull request: https://github.com/mozilla/nunjucks/pull/280

    If you want an upstream that fixes this, change your package.json to use: "nunjucks": "git://github.com/amyboyd/nunjucks.git"