How to calculate integer division remainder in jinja2

I'm trying to create 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?

thank

+4
source share
2 answers

You just need to remove {{}} 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!

+6
source

You can use a filter batch:

{% for itens_batched in itens|batch(3) %}
   {% for item in itens_batched %}

   {% endfor %}
{% endfor %}

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

+3
source

Source: https://habr.com/ru/post/1542910/


All Articles