Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 reverse a list

Tags:

python

jinja2

Hey i want to reverse the output of the following. Can you help me?

{% set page 7 %}
{% for i in range(1,6) %}
    {% set back_link = page - i %}
    {{back_link}}
 {% endfor %}

Output: 6 5 4 3 2

Wanted: 2 3 4 5 6

like image 765
BrotCast Avatar asked Aug 15 '17 18:08

BrotCast


1 Answers

You can reverse a list in Jinja2 using the reverse filter

{% for x in mylist|reverse %}
    {{x}}
{% endfor %}

This is compatible with range so you could use:

{% for i in range(1, 10)|reverse %}
    {{i}}
{% endfor %}
like image 62
jsj Avatar answered Nov 15 '22 21:11

jsj