I am trying to do this:
{% for movie in movie_list | sort(movie.rating) %}
But that's not right...the documentation is vague...how do you do this in Jinja2?
Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.
As of version 2.6, Jinja2's built-in sort filter allows you to specify an attribute to sort by:
{% for movie in movie_list|sort(attribute='rating') %}
See http://jinja.pocoo.org/docs/templates/#sort
If you want to sort in ascending order
{% for movie in movie_list|sort(attribute='rating') %}
If you want to sort in descending order
{% for movie in movie_list|sort(attribute='rating', reverse = True) %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With