Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate remainder of an integer division in jinja2

Tags:

jinja2

I'm trying to make a mod in jinja2 but no way.

{% set index = 1 %}

option 1:

{% for .... %}
    {% if {{index % 3 == 0}} %}

    {% endif %}
    {% set index = index  + 1 %}
{% endfor %}

option 2:

{% for .... %}
   {% if index.index is divisibleby 3 %}

   {% endif %}
   {% set index = index  + 1 %}
{% endfor %}

Any idea?

Thanks

like image 344
davisoski Avatar asked Jun 02 '14 11:06

davisoski


2 Answers

You can use the batch filter:

{% for item_batched in items|batch(3) %}
   {% for item in items_batched %}

   {% endfor %}
{% endfor %}

http://jinja.pocoo.org/docs/dev/templates/#batch

like image 129
Francisco Kahil Avatar answered Nov 05 '22 12:11

Francisco Kahil


You just need to remove the {{ }} from your first if statement. This code works...

<!--    {% set index = 9 %} -->
{% set index = 10 %}
    {% if index % 3 == 0 %}hi
    {% endif %}
{% set index = index  + 1 %}

Hope this helps!

like image 13
Andrew Kloos Avatar answered Nov 05 '22 10:11

Andrew Kloos