Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jinja2 first x items in for ... if loop

I have the following loop in my jinja2 template

{% for item in list if item.author == 'bob' %}

I am trying to get the first 5 items who have bob as an author.

I tried doing

{% for item in list if item.author == 'bob' and loop.index <= 5 %}

but it returned an undefined error.

How to make it work?

like image 965
applechief Avatar asked Sep 11 '12 11:09

applechief


1 Answers

EDIT:

you can simply nest the expressions?, i.e.

{% for item in list if item.author == 'bob' %}
    {% if loop.index <= 5 %}
       do something
    {% endif %}
{% endfor %}
like image 199
olly_uk Avatar answered Sep 19 '22 21:09

olly_uk