mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 635 forks source link

for-loop doesn't set the loop local variable in context #1403

Open prateekbansal opened 2 years ago

prateekbansal commented 2 years ago

I am trying to write a Custom Extension which gets invoked inside the for-loop. Here in the example below the sectionTop2 variable is an array something like:

[{
    "sectionLevel3": "something"
}]

My template code looks like this:

{%- for oneSectionTop2 in sectionTop2 %}
      {% val1 "oneSectionTop2.sectionLevel3" %}{% endVal1 %}
{%- endfor %}

where I have a custom extension "val1"

In the run function, I was expecting the context to have oneSectionTop2 property available, but it isn't there. The property sectionTop2 is present though.

I want to know if this is expected or am I doing something wrong?

johntom commented 2 years ago

Hi, If you have control over array I think your solution is to do the following payload emulation {data:[{ title: "foo", id: 1 }, { title: "bar", id: 2}]} {%- for val in data%}

{{val.title}} / {{val.id}}

{%- endfor %} HTH