<p>If you wish to use divisibleby to solve this problem, the key is in how you use the <code>loop.index</code> and <code>loop.index0</code> variables.</p>

<ul>
<li><code>loop.index</code> counts the iterations in your loop starting with the number 1</li>
<li><code>loop.index0</code> counts the iterations in your loop starting with the number 0</li>
</ul>

<p>You can test this with an odd number of items using the range function:</p>

<pre><code>{# Loop 3 times  #}
{% for i in 1..3 %}

  {# If index0 is divisible by 2, output the starting row #}
  {% if loop.index0 is divisible by(2) %}
    &lt;div class="row"&gt;
  {% endif %}

    {# Output the content #}
    &lt;div class="block_txt"&gt;&lt;/div&gt;

  {# If index is divisible by 2, output the ending row, and
     if it is the last loop, make sure we close the open row regardless #}
  {% if loop.index is divisible by(2) or loop.last %}
    &lt;/div&gt;
  {% endif %}﻿

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

<p>And will get your desired output and ensure your last row div element gets closed:</p>

<pre><code>&lt;div class="row"&gt;
    &lt;div class="block_txt"&gt;&lt;/div&gt;
    &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="row"&gt;       
    &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>You can read more on the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="noreferrer">Twig loop variable</a> and the <a href="http://twig.sensiolabs.org/doc/functions/range.html" rel="noreferrer">Twig range function</a> in the Twig docs.</p>

<p>For your use case, you can then just update the range function to use the variable you need to loop through:</p>

<pre><code>{% for entry in entriesInYear %}

  {% if loop.index0 is divisible by(2) %}
    &lt;div class="row"&gt;
  {% endif %}

    &lt;div class="block_txt"&gt;&lt;/div&gt;

  {% if loop.index is divisible by(2) or loop.last %}
    &lt;/div&gt;
  {% endif %}﻿

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

CC BY-SA 3.0
2014-06-26T10:47:53.707Z
Thanks for the input. I changed to the batch filter, because the code is much smaller. But the learning effect of posts like this is still big :)
KSPR
CC BY-SA 3.0
2014-06-26T04:46:20.117Z
2014-06-26T04:46:20.117Z
Ben Parizek
<p>Instead of using the <a href="http://twig.sensiolabs.org/doc/tests/divisibleby.html"><code>divisibleby</code>-function</a> you can use the <a href="http://twig.sensiolabs.org/doc/filters/batch.html"><code>batch</code>-filter</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{% for row in entriesInYear|batch(2) %}
&lt;div class="row"&gt;
    {% for entry in row %}
        &lt;div class="block_txt"&gt;&lt;/div&gt;
    {% endfor %}
&lt;/div&gt;
{% endfor %}
</code></pre>

<p>If you have 3 entries in <code>entriesInYear</code> this code would output:</p>

<pre><code>&lt;div class="row"&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="row"&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2014-11-18T10:24:27.930Z
This is flippin awesome!
Florian
CC BY-SA 3.0
2015-09-17T16:32:36.487Z
Very neat trick. Worked perfectly!
Anna_MediaGirl
CC BY-SA 3.0
2014-06-25T16:33:35.150Z
2014-06-25T16:48:12.297Z
2014-06-25T16:48:12.297Z
Victor
<p>I try to write a row in which there are two columns:</p>

<pre class="lang-html prettyprint-override"><code>&lt;div class="row"&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="row"&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
   &lt;div class="block_txt"&gt;&lt;/div&gt;
&lt;/div&gt;
{# and so on and so on #}
</code></pre>

<p>Now maybe there is only one <code>block_txt</code> div at the end so I try to wrap the row tag in a <code>devisibleby</code> conditional to only write the row tag every second time, or at the end.
It looks like this:</p>

<pre class="lang-twig prettyprint-override"><code>{% for entry in entriesInYear %}
   {% if loop.index is divisibleby(2) or loop.first %}&lt;div class="row"&gt;{% endif %}
      &lt;div class="block_txt"&gt;This gets repeated n times&lt;/div&gt;
   {% if loop.index is divisibleby(2) or loop.last  %}&lt;/div&gt;{% endif %}
{% endfor %}
</code></pre>

<p>But that doesn't work the way I want. Every third block_txt isn't wrapped in a row. How do I have to math this? Or is there a better way to write the whole piece?</p>

<templating><conditional>
How do I use divisibleby() in a for loop to display two columns?
# How do I use divisibleby() in a for loop to display two columns?

(Asked by: username_0 on 2014-06-25)

I try to write a row in which there are two columns:

   
   


   
   

{# and so on and so on #}

Now maybe there is only one block_txt div at the end so I try to wrap the row tag in a devisibleby conditional to only write the row tag every second time, or at the end.
It looks like this:
{% for entry in entriesInYear %}
   {% if loop.index is divisibleby(2) or loop.first %}{% endif %}
      This gets repeated n times
   {% if loop.index is divisibleby(2) or loop.last  %}{% endif %}
{% endfor %}

But that doesn't work the way I want. Every third block_txt isn't wrapped in a row. How do I have to math this? Or is there a better way to write the whole piece?

**Tags:** templating, conditional


---

## 2 answers

### Answer by username_2 on 2014-06-25. Score: 13 | Accepted answer

Instead of using the divisibleby-function you can use the batch-filter:
{% for row in entriesInYear|batch(2) %}

    {% for entry in row %}
        
    {% endfor %}

{% endfor %}

If you have 3 entries in entriesInYear this code would output:

> **Comments:**
> **username_3**: This is flippin awesome!  
> **username_4**: Very neat trick. Worked perfectly!  

---

### Answer by username_1 on 2014-06-26. Score: 10

If you wish to use divisibleby to solve this problem, the key is in how you use the loop.index and loop.index0 variables.

loop.index counts the iterations in your loop starting with the number 1
loop.index0 counts the iterations in your loop starting with the number 0

You can test this with an odd number of items using the range function:
{# Loop 3 times  #}
{% for i in 1..3 %}

  {# If index0 is divisible by 2, output the starting row #}
  {% if loop.index0 is divisible by(2) %}
    
  {% endif %}

    {# Output the content #}
    

  {# If index is divisible by 2, output the ending row, and
     if it is the last loop, make sure we close the open row regardless #}
  {% if loop.index is divisible by(2) or loop.last %}
    
  {% endif %}﻿

{% endfor %}

And will get your desired output and ensure your last row div element gets closed:

    
    

       
    


You can read more on the Twig loop variable and the Twig range function in the Twig docs.
For your use case, you can then just update the range function to use the variable you need to loop through:
{% for entry in entriesInYear %}

  {% if loop.index0 is divisible by(2) %}
    
  {% endif %}

    

  {% if loop.index is divisible by(2) or loop.last %}
    
  {% endif %}﻿

{% endfor %}

> **Comments:**
> **username_0**: Thanks for the input. I changed to the batch filter, because the code is much smaller. But the learning effect of posts like this is still big :)  

---

