0

I am developing a web using flask. I have two class objects from models.py. I need to loop over both of them at the same time in my HTML file using Jinja2.

For example: I want to have the following code in jinja2 format:

for i,j in zip(items, team):
    a= i+j

Want to convert it to jinja2 format:

{% for i,j  in zip(items, teams) %}
    {% a=i+j %}
{% endfor%}

What is the problem with this jinja2 code?

vallentin
  • 23,478
  • 6
  • 59
  • 81
Jasper
  • 11
  • 1

2 Answers2

0

Jinja doesn't actually have a zip global function. So you need to make it available by doing:

app.jinja_env.globals.update(zip=zip)

Additionally, assignments require using the set keyword, e.g. {% set a = i + j %}

{% for i, j in items %}
    {% set a = i + j %}

{% endfor %}

See also: "zip(list1, list2) in Jinja2?"

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • Thank you , it was very helpful. I just did this and worked: in __init__.py : app.jinja_env.filters['zip'] = zip {% for i, j in items | zip(teams) %} {% set a = i + j %} {% endfor %} – Jasper Jan 09 '21 at 06:22
0

Thank you , it was very helpful. I just did this and worked: in init.py file I add this:

app.jinja_env.filters['zip'] = zip

in the index.html:

{% for i, j in items | zip(teams) %}
 {% set a = i + j %}
{% endfor %}
Jasper
  • 11
  • 1