using cycle with unknown number of entries 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(['&lt;div class="row"&gt;', '', '', ''], loop.index0) | raw }}
... stuff....
{{ cycle(['', '', '', '&lt;/div&gt;'], loop.index0) | raw }}
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, '') %}
&lt;div class="row"&gt;
    {% for column in row %}
        &lt;p&gt;{{ column }}&lt;/p&gt;
    {% endfor %}
&lt;/div&gt;
{% 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 '').