Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T20:03:31.013Z
Answers.LastActivityDate: 2015-07-02T20:03:31.013Z
Answers.UserDisplayName: megatrond
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-02T15:14:25.643Z
Answers.LastActivityDate: 2015-07-02T15:14:25.643Z
Answers.UserDisplayName: Bill Criswell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-02T13:07:48.193Z
Comments.Text: 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
Comments.UserDisplayName: AbbeyDesign
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-02T15:10:07.417Z
Comments.Text: Also this question which makes use of twigs batch filter which does exactly this http://craftcms.stackexchange.com/a/6776/643
Comments.UserDisplayName: Alec Ritson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-02T15:16:31.483Z
Comments.Text: I did this the other day - batch will definitely do what you want. http://twig.sensiolabs.org/doc/filters/batch.html
Comments.UserDisplayName: darylknight
Tags: <templating><forloop>
Title: twig for loop put every 2 elements in a new container
ThreadText: # 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 %}
  
      <div> {{ block.text }} </div>
  
  {% endfor %}
  
  ```
  
  That actually outputs something like:
  
  ```
  <div> one </div>
  <div> two </div>
  <div> three </div>
  <div> ... </div>
  
  ```
  
  What I want is, every 2 div, put them in a new container, like:
  
  ```
  <div class="container">
      <div> one </div>
      <div> two </div>
  </div>
  <div class="container">
      <div> three </div>
      <div> ... </div>
  </div>
  
  ```
  
  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 %}
      <div>
    {% endif %}
     {# other stuff #}
    {% if loop.index0 % 2 == 1 or loop.last %}
      </div>
    {% endif %}
  {% endfor %}
  
  ```
  
  Or, more generally,
  
  ```
  {% set cols = 2 %}
  {% for block in blocks %}
    {% if loop.index0 % cols == 0 %}
      <div>
    {% endif %}
     {# other stuff #}
    {% if (loop.index0 % cols == cols - 1 or loop.last) %}
      </div>
    {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2015-07-02. Score: 5
  
  I think `batch` that @username_2 mentioned is definitely the way to go.
  
  However, here's another approach for fun.
  
  You can use [`cycle`](http://twig.sensiolabs.org/doc/functions/cycle.html).
  
  ```
  {{ cycle('<div>', '') }}
    {{ foo }}
  {{ cycle('</div>', '') }}
  
  ```
  
  But let me repeat: I really think [`batch`](http://twig.sensiolabs.org/doc/filters/batch.html) is the way to go.
  
  
  ---
  
  