D
D
Denis2016-11-16 18:22:51
Jinja
Denis, 2016-11-16 18:22:51

How to get out of a loop in jinja2?

Hello. Tell me how to exit the loop in jinja2. The description of the task is as follows:
A response is received from an Ajax request, the response (object) contains a property whose value is an array. I want to display the elements of this array, but there is one condition, the array elements can contain the same values, in which case I need to display only the first element. With the current implementation, if all elements are the same, then they are not displayed at all.

{% for tariff_tag in data.sub_tariff if not tariff_tag.tariff__tariff_category == 'amedia-subscription' %}
       <a>link</a>
{% endfor %}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2016-11-17
@den0820

Thank you all for your comments. The actual solution was to simply extend jinja with the loopcontrols module

app.jinja_env.add_extension('jinja2.ext.loopcontrols')

U
un1t, 2016-11-17
@un1t

Process the data to the template (in a view for example) as you need. And in the template just output in the loop and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question