我有一个可变的城市名称列表,我希望将其平均分成4列。我有一些解决方案,但它看起来不堪重负和肮脏。什么是最好最简单的方法?
我的解决方案在这里:
{% set cities_in_column = cities|length/4|int %}
{% set step=0 %}
<div class="four columns">
{% for city in cities|sort %}
{% if step > cities_in_column %}
{% set step = 0 %}
</div>
<div class="four columns">
{% endif %}
<h5><a href="/city/{{ city.url }}">{{ city.name }}</a> <span style="float:right;">({{ city.users_count }})</span></h5>
{% set step=step + 1 %}
{% endfor %}
</div>
答案 0 :(得分:12)
您正在寻找slices
过滤器:
{% 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 %}
答案 1 :(得分:0)
这是CSS *的工作:
<ol style="column-count: 4;">
{% for city in cities|sort %}
<li>
<a href="/city/{{ city.url }}">{{ city.name }}</a> ({{ city.users_count }})
</li>
{% endfor %}
</ol>
(*是column-count
,float
,flex
等。)