<p>I usually do this like so:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Or, more generally,</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2015-07-02T20:03:31.013Z
2015-07-02T20:03:31.013Z
megatrond
<p>I think <code>batch</code> that @Alec Ritson mentioned is definitely the way to go.</p>

<p>However, here's another approach for fun.</p>

<p>You can use <a href="http://twig.sensiolabs.org/doc/functions/cycle.html" rel="noreferrer"><code>cycle</code></a>.</p>

<pre><code>{{ cycle('&lt;div&gt;', '') }}
  {{ foo }}
{{ cycle('&lt;/div&gt;', '') }}
</code></pre>

<p>But let me repeat: I really think <a href="http://twig.sensiolabs.org/doc/filters/batch.html" rel="noreferrer"><code>batch</code></a> is the way to go.</p>

CC BY-SA 3.0
2015-07-02T15:14:25.643Z
2015-07-02T15:14:25.643Z
Bill Criswell
<p>I have this loop:</p>

<pre><code>{% for div in myHandleHere %}

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

{% endfor %}
</code></pre>

<p>That actually outputs something like:</p>

<pre><code>&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;
</code></pre>

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

<pre><code>&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;
</code></pre>

<p>Please help</p>

CC BY-SA 3.0
2015-07-02T13:07:48.193Z
You can find a similar question/answer here:

http://craftcms.stackexchange.com/questions/472/how-do-i-use-divisibleby-in-a-for-loop-to-display-two-columns/499#499
AbbeyDesign
CC BY-SA 3.0
2015-07-02T15:10:07.417Z
Also this question which makes use of twigs batch filter which does exactly this http://craftcms.stackexchange.com/a/6776/643
Alec Ritson
CC BY-SA 3.0
2015-07-02T15:16:31.483Z
I did this the other day - batch will definitely do what you want. http://twig.sensiolabs.org/doc/filters/batch.html
darylknight
<templating><forloop>
twig for loop put every 2 elements in a new container
# twig for loop put every 2 elements in a new container

(Asked by: username_0 on 2015-07-02)

I have this loop:
{% for div in myHandleHere %}

     {{ block.text }} 

{% endfor %}

That actually outputs something like:
 one 
 two 
 three 
 ... 

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

     one 
     two 


     three 
     ... 


Please help

**Tags:** templating, forloop
> **Comments:**
> **username_1**: You can find a similar question/answer here:
 
 http://craftcms.stackexchange.com/questions/472/how-do-i-use-divisibleby-in-a-for-loop-to-display-two-columns/499#499  
> **username_2**: Also this question which makes use of twigs batch filter which does exactly this http://craftcms.stackexchange.com/a/6776/643  
> **username_3**: I did this the other day - batch will definitely do what you want. http://twig.sensiolabs.org/doc/filters/batch.html  

---

## 2 answers

### Answer by username_4 on 2015-07-02. Score: 9

I usually do this like so:
{% for block in blocks %}
  {% if loop.index0 % 2 == 0 %}
    
  {% endif %}
   {# other stuff #}
  {% if loop.index0 % 2 == 1 or loop.last %}
    
  {% endif %}
{% endfor %}

Or, more generally,
{% set cols = 2 %}
{% for block in blocks %}
  {% if loop.index0 % cols == 0 %}
    
  {% endif %}
   {# other stuff #}
  {% if (loop.index0 % cols == cols - 1 or loop.last) %}
    
  {% endif %}
{% endfor %}


---

### Answer by username_5 on 2015-07-02. Score: 5

I think batch that @Alec Ritson mentioned is definitely the way to go.
However, here's another approach for fun.
You can use cycle.
{{ cycle('', '') }}
  {{ foo }}
{{ cycle('', '') }}

But let me repeat: I really think batch is the way to go.


---

