<p>This is how I usually solve this problem, notice the explicit check for the last element in the list. You can set <code>cols</code> to whatever number of columns you want.</p>

<pre><code>{% set cols = 4 %}
{% for thing in things %}
  {% if loop.index0 % cols == 0 %}
    &lt;div class="row"&gt;
  {% endif %}
  {# other stuff #}
  {% if (loop.index0 % cols == cols - 1 or loop.last) %}
    &lt;/div&gt;
  {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-04-21T07:22:23.543Z
2017-04-21T07:22:23.543Z
megatrond
<p>Maybe you can use Twig's batch for this?</p>

<p><a href="https://twig.sensiolabs.org/doc/2.x/filters/batch.html" rel="noreferrer">Twig Batch</a></p>

<pre><code>{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
{% for row in items|batch(3, '') %}
&lt;div class="row"&gt;
    {% for column in row %}
        &lt;p&gt;{{ column }}&lt;/p&gt;
    {% endfor %}
&lt;/div&gt;
{% endfor %}
</code></pre>

<p>Batch lets you have something to fall back on when you have less items. In this case there are 3 items within my row and if there will be 2, it will output what I have in the '', in this case nothing. (but I can put what I want in that '').</p>

CC BY-SA 3.0
2017-04-21T18:25:03.560Z
Nice! Just when you think you have a good handle on all the cool stuff Twig does, you forget there's ``batch``...
RitterKnight
CC BY-SA 3.0
2017-04-20T23:31:24.440Z
2017-04-20T23:44:34.313Z
2017-04-20T23:44:34.313Z
AbbeyDesign
<p>I'm using cycle to open and close a div for a row of content. It works great when there are more than a certain number of entries - in this case 4, but doesn't work when there are fewer than that. Is there a way to make this work so it always closes the div even if there are fewer entries than specified in the code? </p>

<pre><code>{{ cycle(['&lt;div class="row"&gt;', '', '', ''], loop.index0) | raw }}
... stuff....
{{ cycle(['', '', '', '&lt;/div&gt;'], loop.index0) | raw }}
</code></pre>

<cycle>
using cycle with unknown number of entries
# using cycle with unknown number of entries

(Asked by: username_0 on 2017-04-20)

I'm using cycle to open and close a div for a row of content. It works great when there are more than a certain number of entries - in this case 4, but doesn't work when there are fewer than that. Is there a way to make this work so it always closes the div even if there are fewer entries than specified in the code? 
{{ cycle(['', '', '', ''], loop.index0) | raw }}
... stuff....
{{ cycle(['', '', '', ''], loop.index0) | raw }}

**Tags:** cycle


---

## 2 answers

### Answer by username_1 on 2017-04-21. Score: 3 | Accepted answer

This is how I usually solve this problem, notice the explicit check for the last element in the list. You can set cols to whatever number of columns you want.
{% set cols = 4 %}
{% for thing in things %}
  {% if loop.index0 % cols == 0 %}
    
  {% endif %}
  {# other stuff #}
  {% if (loop.index0 % cols == cols - 1 or loop.last) %}
    
  {% endif %}
{% endfor %}


---

### Answer by username_2 on 2017-04-20. Score: 5

Maybe you can use Twig's batch for this?
Twig Batch
{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
{% for row in items|batch(3, '') %}

    {% for column in row %}
        {{ column }}
    {% endfor %}

{% endfor %}

Batch lets you have something to fall back on when you have less items. In this case there are 3 items within my row and if there will be 2, it will output what I have in the '', in this case nothing. (but I can put what I want in that '').

> **Comments:**
> **username_3**: Nice! Just when you think you have a good handle on all the cool stuff Twig does, you forget there's ``batch``...  

---

