twig for loop put every 2 elements in a new container I have this loop:

{% for div in myHandleHere %}

    &lt;div&gt; {{ block.text }} &lt;/div&gt;

{% endfor %}


That actually outputs something like:

&lt;div&gt; one &lt;/div&gt;
&lt;div&gt; two &lt;/div&gt;
&lt;div&gt; three &lt;/div&gt;
&lt;div&gt; ... &lt;/div&gt;


What I want is, every 2 div, put them in a new container, like:

&lt;div class="container"&gt;
    &lt;div&gt; one &lt;/div&gt;
    &lt;div&gt; two &lt;/div&gt;
&lt;/div&gt;
&lt;div class="container"&gt;
    &lt;div&gt; three &lt;/div&gt;
    &lt;div&gt; ... &lt;/div&gt;
&lt;/div&gt;


Please help
I usually do this like so:

{% for block in blocks %}
  {% if loop.index0 % 2 == 0 %}
    &lt;div&gt;
  {% endif %}
   {# other stuff #}
  {% if loop.index0 % 2 == 1 or loop.last %}
    &lt;/div&gt;
  {% endif %}
{% endfor %}


Or, more generally,

{% set cols = 2 %}
{% for block in blocks %}
  {% if loop.index0 % cols == 0 %}
    &lt;div&gt;
  {% endif %}
   {# other stuff #}
  {% if (loop.index0 % cols == cols - 1 or loop.last) %}
    &lt;/div&gt;
  {% endif %}
{% endfor %}