mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

Home Page:https://mozilla.github.io/nunjucks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

prateekbansal opened this issue · comments

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?

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