<p>To my understanding this should work for a matrix field (called imageLinkBoxes) that contains only one type of block (<code>imageLinkBox</code>) that has 3 fields (<code>imageLinkBox.linkText</code>, <code>imageLinkBox.linkUrl</code> and <code>imageLinkBox.text</code>)</p>

<pre><code>{% set aboutInfo = entry.imageLinkBoxes %} {# get all the blocks in the matrix #}

{% if aboutInfo|length %} {# check we have at least one block in the matrix #}

    {% set infoGrouped = aboutInfo|batch(3) %} {# group them in groups of 3 blocks #}

    {% for group in infoGrouped %}  {# loop through all the groups #}

        {% set row = 'row--' ~ loop.index %} {# set the class #}

        &lt;div class="row {{ row }}"&gt; {# open the row #}

        {% for block in group %} {# loop through all the blocks in current group #}

            &lt;div class="span4"&gt;
                &lt;div class="icon-box-2"&gt;    
                    &lt;i class="ifc-tv_show"&gt;&lt;/i&gt;
                    &lt;div class="icon-box-content"&gt;
                        &lt;h3 class="text-uppercase text-center"&gt;
                            &lt;a href="{{ block.linkUrl }}"&gt;{{ block.linkText }}&lt;/a&gt; 
                        &lt;/h3&gt;
                        &lt;p&gt;{{ block.text }}&lt;/p&gt;
                    &lt;/div&gt;  
                &lt;/div&gt;
               &lt;!-- /.icon-box-2 --&gt;               
            &lt;/div&gt;
            &lt;!-- /.span4--&gt;

        {% endfor %}

        &lt;/div&gt; {# close the row #}
        &lt;!-- /.row--&gt;

    {% endfor %}

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

<p>If for some reason you have more than one type of block in the matrix, then you would need to do a change:</p>

<p>Replace:</p>

<pre><code>{% set aboutInfo = entry.imageLinkBoxes %}
</code></pre>

<p>With:</p>

<pre><code>{% set aboutInfo = entry.imageLinkBoxes.type('imageLinkBox') %}
</code></pre>

<p>That would filter the blocks to only those of the type <code>imageLinkBox</code>.</p>

CC BY-SA 3.0
2015-10-19T07:17:58.883Z
2015-10-19T16:02:22.223Z
2015-10-19T16:02:22.223Z
Ovi Savescu
<p>I need to output a row every three items in a matrix. Here is what I have so far. The problem is that this outputs a row every three items but it duplicates each matrix item three times and puts them in a row rather than taking the items as they come. Any help here would be very much appreciated Thanks.</p>

<pre><code>{% set aboutInfo = entry.imageLinkBoxes %}
{% set infoGrouped = aboutInfo|batch(3) %}

{% for group in infoGrouped %}
    {% for block in entry.imageLinkBoxes %}

        {% if block.type == "imageLinkBox" %}

            {% set row = 'row--' ~ loop.index %}

            &lt;div class="row {{ row }}"&gt;
                {% for entry in group %}

                    &lt;div class="span4"&gt;
                        &lt;div class="icon-box-2"&gt;    
                            &lt;i class="ifc-tv_show"&gt;&lt;/i&gt;
                            &lt;div class="icon-box-content"&gt;
                                &lt;h3 class="text-uppercase text-center"&gt;
                                    &lt;a href="#"&gt;{{ block.entry.first().title }}&lt;/a&gt;
                                &lt;/h3&gt;
                                &lt;p&gt;{{ block.text }}&lt;/p&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;               
                    &lt;/div&gt;

                {% endfor %}
            &lt;/div&gt;

        {% endif %}

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

CC BY-SA 3.0
2015-10-17T16:45:17.287Z
Your code is a little bit confusing, but your issue is with the nested loops and where you put certain parts of the code. Can you describe your Matrix field content? What, exactly are the `items` you want to put in each `.row`? Are they entries inside Matrix blocks?
Mats Mikkel Rummelhoff
<matrix><forloop><batch>
Create a new row every 3 items in a matrix
# Create a new row every 3 items in a matrix

(Asked by: username_0 on 2015-10-17)

I need to output a row every three items in a matrix. Here is what I have so far. The problem is that this outputs a row every three items but it duplicates each matrix item three times and puts them in a row rather than taking the items as they come. Any help here would be very much appreciated Thanks.
{% set aboutInfo = entry.imageLinkBoxes %}
{% set infoGrouped = aboutInfo|batch(3) %}

{% for group in infoGrouped %}
    {% for block in entry.imageLinkBoxes %}

        {% if block.type == "imageLinkBox" %}

            {% set row = 'row--' ~ loop.index %}

            
                {% for entry in group %}

                    
                            
                            
                            
                                
                                    {{ block.entry.first().title }}
                                
                                {{ block.text }}
                            
                                       
                    

                {% endfor %}
            

        {% endif %}

    {% endfor %}
{% endfor %}

**Tags:** matrix, forloop, batch

> **Comments:**
> **username_1**: Your code is a little bit confusing, but your issue is with the nested loops and where you put certain parts of the code. Can you describe your Matrix field content? What, exactly are the `items` you want to put in each `.row`? Are they entries inside Matrix blocks?  

---

## 1 answers

### Answer by username_2 on 2015-10-19. Score: 4 | Accepted answer

To my understanding this should work for a matrix field (called imageLinkBoxes) that contains only one type of block (imageLinkBox) that has 3 fields (imageLinkBox.linkText, imageLinkBox.linkUrl and imageLinkBox.text)
{% set aboutInfo = entry.imageLinkBoxes %} {# get all the blocks in the matrix #}

{% if aboutInfo|length %} {# check we have at least one block in the matrix #}

    {% set infoGrouped = aboutInfo|batch(3) %} {# group them in groups of 3 blocks #}

    {% for group in infoGrouped %}  {# loop through all the groups #}

        {% set row = 'row--' ~ loop.index %} {# set the class #}

         {# open the row #}

        {% for block in group %} {# loop through all the blocks in current group #}

            
                    
                    
                    
                        
                            {{ block.linkText }} 
                        
                        {{ block.text }}
                      
                
                              
            
            

        {% endfor %}

         {# close the row #}
        

    {% endfor %}

{% endif %}

If for some reason you have more than one type of block in the matrix, then you would need to do a change:
Replace:
{% set aboutInfo = entry.imageLinkBoxes %}

With:
{% set aboutInfo = entry.imageLinkBoxes.type('imageLinkBox') %}

That would filter the blocks to only those of the type imageLinkBox.


---

