Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-26T10:47:53.707Z
Answers.Text: 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 :)
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-26T04:46:20.117Z
Answers.LastActivityDate: 2014-06-26T04:46:20.117Z
Answers.UserDisplayName: Ben Parizek
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-18T10:24:27.930Z
Answers.Text: This is flippin awesome!
Answers.UserDisplayName: Florian
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-17T16:32:36.487Z
Answers.Text: Very neat trick. Worked perfectly!
Answers.UserDisplayName: Anna_MediaGirl
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-25T16:33:35.150Z
Answers.LastActivityDate: 2014-06-25T16:48:12.297Z
Answers.LastEditDate: 2014-06-25T16:48:12.297Z
Answers.UserDisplayName: Victor
Body: <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>
  
Tags: <templating><conditional>
Title: How do I use divisibleby() in a for loop to display two columns?
ThreadText: # 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:
  
  ```html
  <div class="row">
     <div class="block_txt"></div>
     <div class="block_txt"></div>
  </div>
  <div class="row">
     <div class="block_txt"></div>
     <div class="block_txt"></div>
  </div>
  {# 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:
  
  ```twig
  {% for entry in entriesInYear %}
     {% if loop.index is divisibleby(2) or loop.first %}<div class="row">{% endif %}
        <div class="block_txt">This gets repeated n times</div>
     {% if loop.index is divisibleby(2) or loop.last  %}</div>{% 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](http://twig.sensiolabs.org/doc/tests/divisibleby.html) you can use the [`batch`-filter](http://twig.sensiolabs.org/doc/filters/batch.html):
  
  ```twig
  {% for row in entriesInYear|batch(2) %}
  <div class="row">
      {% for entry in row %}
          <div class="block_txt"></div>
      {% endfor %}
  </div>
  {% endfor %}
  
  ```
  
  If you have 3 entries in `entriesInYear` this code would output:
  
  ```
  <div class="row">
     <div class="block_txt"></div>
     <div class="block_txt"></div>
  </div>
  <div class="row">
     <div class="block_txt"></div>
  </div>
  
  ```
  
  > **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) %}
      <div class="row">
    {% endif %}
  
      {# Output the content #}
      <div class="block_txt"></div>
  
    {# 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 %}
      </div>
    {% endif %}﻿
  
  {% endfor %}
  
  ```
  
  And will get your desired output and ensure your last row div element gets closed:
  
  ```
  <div class="row">
      <div class="block_txt"></div>
      <div class="block_txt"></div>
  </div>
  <div class="row">       
      <div class="block_txt"></div>
  </div>
  
  ```
  
  You can read more on the [Twig loop variable](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable) and the [Twig range function](http://twig.sensiolabs.org/doc/functions/range.html) 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) %}
      <div class="row">
    {% endif %}
  
      <div class="block_txt"></div>
  
    {% if loop.index is divisible by(2) or loop.last %}
      </div>
    {% 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 :)  
  
  ---
  
  