Break items into columns evenly in Jinja2 template
You are looking for the slices
filter:
{% for column in cities | sort | slice(4) -%}
<div class="four columns">
{%- for city in column -%}
<h5><a href="/city/{{ city.url}}">{{ city.name }}</a>
<span style="float:right;">({{ city.users_count }})</span></h5>
{%- endfor -%}
</div>
{%- endfor %}
There is also a complement to slices
called batch
that provides runs of n
(rather than splitting up the iterable into n
groups).