Answers.Body: <p>This is how I usually solve this problem, notice the explicit check for the last element in the list. You can set <code>cols</code> to whatever number of columns you want.</p>
  
  <pre><code>{% set cols = 4 %}
  {% for thing in things %}
    {% if loop.index0 % cols == 0 %}
      &lt;div class="row"&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: 2017-04-21T07:22:23.543Z
Answers.LastActivityDate: 2017-04-21T07:22:23.543Z
Answers.UserDisplayName: megatrond
Answers.Body: <p>Maybe you can use Twig's batch for this?</p>
  
  <p><a href="https://twig.sensiolabs.org/doc/2.x/filters/batch.html" rel="noreferrer">Twig Batch</a></p>
  
  <pre><code>{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
  {% for row in items|batch(3, '') %}
  &lt;div class="row"&gt;
      {% for column in row %}
          &lt;p&gt;{{ column }}&lt;/p&gt;
      {% endfor %}
  &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>Batch lets you have something to fall back on when you have less items. In this case there are 3 items within my row and if there will be 2, it will output what I have in the '', in this case nothing. (but I can put what I want in that '').</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-21T18:25:03.560Z
Answers.Text: Nice! Just when you think you have a good handle on all the cool stuff Twig does, you forget there's ``batch``...
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-20T23:31:24.440Z
Answers.LastActivityDate: 2017-04-20T23:44:34.313Z
Answers.LastEditDate: 2017-04-20T23:44:34.313Z
Answers.UserDisplayName: AbbeyDesign
Body: <p>I'm using cycle to open and close a div for a row of content. It works great when there are more than a certain number of entries - in this case 4, but doesn't work when there are fewer than that. Is there a way to make this work so it always closes the div even if there are fewer entries than specified in the code? </p>
  
  <pre><code>{{ cycle(['&lt;div class="row"&gt;', '', '', ''], loop.index0) | raw }}
  ... stuff....
  {{ cycle(['', '', '', '&lt;/div&gt;'], loop.index0) | raw }}
  </code></pre>
  
Tags: <cycle>
Title: using cycle with unknown number of entries
ThreadText: # using cycle with unknown number of entries
  
  (Asked by: username_0 on 2017-04-20)
  
  I'm using cycle to open and close a div for a row of content. It works great when there are more than a certain number of entries - in this case 4, but doesn't work when there are fewer than that. Is there a way to make this work so it always closes the div even if there are fewer entries than specified in the code?
  
  ```
  {{ cycle(['<div class="row">', '', '', ''], loop.index0) | raw }}
  ... stuff....
  {{ cycle(['', '', '', '</div>'], loop.index0) | raw }}
  
  ```
  
  **Tags:** cycle
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-21. Score: 3 | Accepted answer
  
  This is how I usually solve this problem, notice the explicit check for the last element in the list. You can set `cols` to whatever number of columns you want.
  
  ```
  {% set cols = 4 %}
  {% for thing in things %}
    {% if loop.index0 % cols == 0 %}
      <div class="row">
    {% endif %}
    {# other stuff #}
    {% if (loop.index0 % cols == cols - 1 or loop.last) %}
      </div>
    {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-04-20. Score: 5
  
  Maybe you can use Twig's batch for this?
  
  [Twig Batch](https://twig.sensiolabs.org/doc/2.x/filters/batch.html)
  
  ```
  {% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
  {% for row in items|batch(3, '') %}
  <div class="row">
      {% for column in row %}
          <p>{{ column }}</p>
      {% endfor %}
  </div>
  {% endfor %}
  
  ```
  
  Batch lets you have something to fall back on when you have less items. In this case there are 3 items within my row and if there will be 2, it will output what I have in the '', in this case nothing. (but I can put what I want in that '').
  
  > **Comments:**
  > **username_3**: Nice! Just when you think you have a good handle on all the cool stuff Twig does, you forget there's ``batch``...  
  
  ---
  
  