Answers.Body: <p>The array slice something you are looking for is Twig's <a href="http://twig.sensiolabs.org/doc/filters/batch.html"><code>batch</code></a> filter. Here's how you'd set up the groups and use <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable"><code>loop.index</code></a> to generate the class name.</p>
  
  <pre><code>{# Use the `find()` method to return an array of EntryModels #}
  {% set selectedWork = craft.entries.section('selectedWork').find() %}
  
  {# Group entries using the `batch` filter #}
  {% set selectedWorkGrouped = selectedWork|batch(9) %}
  
  {% for group in selectedWorkGrouped %}
  
      {# Set class using the loop index from outer loop #}
      {% set panelClass = 'panel--' ~ loop.index %}
  
      &lt;div class="panel {{ panelClass }}"&gt;
  
          {% for entry in group %}
              &lt;section class="img-grid__item"&gt;
                  {{ entry.title }}
              &lt;/section&gt;
          {% endfor %}
  
      &lt;/div
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-23T19:11:27.920Z
Answers.Text: Wow I didn't know about the batch filter, this solution worked like a charm. Thank you very much.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-23T17:21:23.193Z
Answers.LastActivityDate: 2015-01-23T17:27:44.647Z
Answers.LastEditDate: 2015-01-23T17:27:44.647Z
Answers.UserDisplayName: carlcs
Body: <p>I am trying to loop through all the entries in a section and "group" them into items which are in blocks of 9 then wrap them with some html markup so there will be the following code:</p>
  
  <pre><code>{% set selectedWork = craft.entries.section('selectedWork') %}
  
  &lt;div class="panel panel--1"&gt;
    &lt;article class="img-grid"&gt;
  
      {% for item in selectedWork %}
      &lt;section class="img-grid__item"&gt;
        {{ item.title }}
      &lt;/section&gt;
      {% endfor %}
  
    &lt;/article&gt;
  &lt;/div&gt;&lt;!-- END .panel --&gt;
  </code></pre>
  
  <p>The outer <code>&lt;div class="panel panel-1"&gt;</code> and <code>&lt;article&gt;</code> tags need to wrap round the first 9 items then then next 9 etc. etc. Also the panel--1 needs to increment so that each panel goes in order so <code>panel--1</code> holds 1-9 items then <code>panel--2</code> holds 9-18 items etc. Is this possible using some kind of array splice or something?</p>
  
Tags: <forloop>
Title: Group looped entries into blocks and wrap in markup every x number of loops
ThreadText: # Group looped entries into blocks and wrap in markup every x number of loops
  
  (Asked by: username_0 on 2015-01-23)
  
  I am trying to loop through all the entries in a section and "group" them into items which are in blocks of 9 then wrap them with some html markup so there will be the following code:
  
  ```
  {% set selectedWork = craft.entries.section('selectedWork') %}
  
  <div class="panel panel--1">
    <article class="img-grid">
  
      {% for item in selectedWork %}
      <section class="img-grid__item">
        {{ item.title }}
      </section>
      {% endfor %}
  
    </article>
  </div><!-- END .panel -->
  
  ```
  
  The outer `<div class="panel panel-1">` and `<article>` tags need to wrap round the first 9 items then then next 9 etc. etc. Also the panel--1 needs to increment so that each panel goes in order so `panel--1` holds 1-9 items then `panel--2` holds 9-18 items etc. Is this possible using some kind of array splice or something?
  
  **Tags:** forloop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-01-23. Score: 10 | Accepted answer
  
  The array slice something you are looking for is Twig's [`batch`](http://twig.sensiolabs.org/doc/filters/batch.html) filter. Here's how you'd set up the groups and use [`loop.index`](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable) to generate the class name.
  
  ```
  {# Use the `find()` method to return an array of EntryModels #}
  {% set selectedWork = craft.entries.section('selectedWork').find() %}
  
  {# Group entries using the `batch` filter #}
  {% set selectedWorkGrouped = selectedWork|batch(9) %}
  
  {% for group in selectedWorkGrouped %}
  
      {# Set class using the loop index from outer loop #}
      {% set panelClass = 'panel--' ~ loop.index %}
  
      <div class="panel {{ panelClass }}">
  
          {% for entry in group %}
              <section class="img-grid__item">
                  {{ entry.title }}
              </section>
          {% endfor %}
  
      </div
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Wow I didn't know about the batch filter, this solution worked like a charm. Thank you very much.  
  
  ---
  
  