Search code examples
pythonflaskmodulowerkzeug

Flask python loop.index modulo


Hi i'm trying to list some elements which divided by a clearfix div after every 3rd element. This is what I tried that doesn't work. How can I do modulo in flask?

{% for i in props %}
<li class="col-xs-4">
    <label class="basic-medium-black">
         <span class="icon-check {% if i in user.props %}active{% endif %}"></span>
         {% filter upper %}{{ i.prop_name }} {% endfilter %}
    </label>
</li>

****************This Part:******
{% if loop.index % 3 == 0 %}
    <div class="clearfix"></div>
{% endif %}
********************************

{% endfor %}

What should I put for modulo insted of "%" ?

Thanks in advance..


Solution

  • Instead of using a modulo, you can also use the the batch() filter to group your items, allowing you to put a clear fix after every group:

    {% for row in props|batch(3) %}
        {% for i in row %}
        <li class="col-xs-4">
            <label class="basic-medium-black">
                 <span class="icon-check {% if i in user.props %}active{% endif %}"></span>
                 {% filter upper %}{{ i.prop_name }} {% endfilter %}
            </label>
        </li>
        {% endfor %}
    
        <div class="clearfix"></div>        
    {% endfor %}