<p>You could use your own variable to count the matrix blocks in your loop. The code could look like this:</p>

<pre><code>{% set rows = 0 %}

&lt;div class="row"&gt;
    {% for event in events %} {# loop through all events #}
        {% if event.startDate|date('Y') == now|date('Y') %} {#check to see if the event's start date is happening this year #}
            {% for row in event.authors %}
                    {% for block in row %}
                        {% switch block.type %}
                            {% case "author" %}

                                {% if rows == 4 %}
                                    &lt;/div&gt;
                                    &lt;div class="row"&gt;
                                    {% set rows = 0 %}
                                {% endif %}


                                &lt;div class="col-md-3 author"&gt; 
                                    {% set image = block.image.first() %}
                                        {% if image|length %}
                                            &lt;img src="{{ image.getUrl('authorThumb') }}"&gt;
                                         {% endif %}
                                    &lt;p class="name"&gt;{{ block.authorName }}&lt;/p&gt;
                                    {% if block.twitterHandle|length %}
                                    &lt;p class="subtitle"&gt;&lt;a href="http://twitter.com/{{ block.twitterHandle }}" target="_blank"&gt;@{{ block.twitterHandle }}&lt;/a&gt;&lt;/p&gt;
                                    {% endif %}
                                     &lt;small&gt;{{ event.startDate|date('m/d/y') }} @ {{ event.startDate|date('g:ia') }} &lt;/small&gt;
                                     &lt;hr&gt;
                                     &lt;p class="bio"&gt;{{ block.bio }}&lt;/p&gt;
                                &lt;/div&gt;

                               {% set rows = rows + 1 %}

                        {% endswitch %}
                    {% endfor %}
            {% endfor %}
        {% endif %}
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>It's important to set the variable outside the for loop, otherwise it does not work.</p>

CC BY-SA 3.0
2016-09-28T00:37:55.567Z
Thank you for answering. As with every variation I've tried on this solution, I tend to get this error: Impossible to access an attribute ("type") on a string variable ("146")
lucaslink
CC BY-SA 3.0
2016-09-28T00:46:01.133Z
Found the issue! You put me on the right track. I like this solution and its totally different than any of the solutions I've seen for this problem. THANK YOU.
lucaslink
CC BY-SA 3.0
2016-09-27T07:19:18.263Z
2016-09-27T07:19:18.263Z
Tom Bauer
<p>I am building a site which uses one content type as its primary holder for content. This is to make content editing easier on the content editor.</p>

<p>All URL's are primarily accessing this one content type, 'events'.</p>

<p>Within 'events' I have a matrix field 'authors'. Authors contains 1 block (also 'authors') with 4 fields, Name, Image, Bio and URL.</p>

<p>I am able to output the matrix blocks fine, but I am not familiar with a way to call all event entries without running them through a for loop first. Doing so causes my row/col layout to break but without batching them into groups of 4, my column layout also breaks. </p>

<p>I have attempted all solutions shown on stackexchange but none of them are suited for this scenario (calling all entries from a section) and my attempts at rewriting it to fit my solution has failed. Any help would be greatly appreciated.</p>

<pre><code>{% for event in events %} {# loop through all events #}
    {% if event.startDate|date('Y') == now|date('Y') %} {#check to see if the event's start date is happening this year #}
        {% for row in event.authors|batch(4) %} {# use batch to create rows #}
            &lt;div class="row"&gt;
                {% for block in row %}
                    {% switch block.type %}
                        {% case "author" %}
                            &lt;div class="col-md-3 author"&gt; 
                                {% set image = block.image.first() %}
                                    {% if image|length %}
                                        &lt;img src="{{ image.getUrl('authorThumb') }}"&gt;
                                     {% endif %}
                                &lt;p class="name"&gt;{{ block.authorName }}&lt;/p&gt;
                                {% if block.twitterHandle|length %}
                                &lt;p class="subtitle"&gt;&lt;a href="http://twitter.com/{{ block.twitterHandle }}" target="_blank"&gt;@{{ block.twitterHandle }}&lt;/a&gt;&lt;/p&gt;
                                {% endif %}
                                 &lt;small&gt;{{ event.startDate|date('m/d/y') }} @ {{ event.startDate|date('g:ia') }} &lt;/small&gt;
                                 &lt;hr&gt;
                                 &lt;p class="bio"&gt;{{ block.bio }}&lt;/p&gt;
                            &lt;/div&gt;
                    {% endswitch %}
                {% endfor %}
            &lt;/div&gt;
        {% endfor %}
    {% endif %}
{% endfor %}
</code></pre>

<p>This is as close as I've gotten, but unfortunately this just creates rows for each entry as opposed to every 4 blocks.</p>

<templating><matrix><loop>
How do I group matrix blocks into batches of 4?
# How do I group matrix blocks into batches of 4?

(Asked by: username_0 on 2016-09-19)

I am building a site which uses one content type as its primary holder for content. This is to make content editing easier on the content editor.
All URL's are primarily accessing this one content type, 'events'.
Within 'events' I have a matrix field 'authors'. Authors contains 1 block (also 'authors') with 4 fields, Name, Image, Bio and URL.
I am able to output the matrix blocks fine, but I am not familiar with a way to call all event entries without running them through a for loop first. Doing so causes my row/col layout to break but without batching them into groups of 4, my column layout also breaks. 
I have attempted all solutions shown on stackexchange but none of them are suited for this scenario (calling all entries from a section) and my attempts at rewriting it to fit my solution has failed. Any help would be greatly appreciated.
{% for event in events %} {# loop through all events #}
    {% if event.startDate|date('Y') == now|date('Y') %} {#check to see if the event's start date is happening this year #}
        {% for row in event.authors|batch(4) %} {# use batch to create rows #}
            
                {% for block in row %}
                    {% switch block.type %}
                        {% case "author" %}
                             
                                {% set image = block.image.first() %}
                                    {% if image|length %}
                                        
                                     {% endif %}
                                {{ block.authorName }}
                                {% if block.twitterHandle|length %}
                                @{{ block.twitterHandle }}
                                {% endif %}
                                 {{ event.startDate|date('m/d/y') }} @ {{ event.startDate|date('g:ia') }} 
                                 
                                 {{ block.bio }}
                            
                    {% endswitch %}
                {% endfor %}
            
        {% endfor %}
    {% endif %}
{% endfor %}

This is as close as I've gotten, but unfortunately this just creates rows for each entry as opposed to every 4 blocks.

**Tags:** templating, matrix, loop


---

## 1 answers

### Answer by username_1 on 2016-09-27. Score: 3 | Accepted answer

You could use your own variable to count the matrix blocks in your loop. The code could look like this:
{% set rows = 0 %}


    {% for event in events %} {# loop through all events #}
        {% if event.startDate|date('Y') == now|date('Y') %} {#check to see if the event's start date is happening this year #}
            {% for row in event.authors %}
                    {% for block in row %}
                        {% switch block.type %}
                            {% case "author" %}

                                {% if rows == 4 %}
                                    
                                    
                                    {% set rows = 0 %}
                                {% endif %}


                                 
                                    {% set image = block.image.first() %}
                                        {% if image|length %}
                                            
                                         {% endif %}
                                    {{ block.authorName }}
                                    {% if block.twitterHandle|length %}
                                    @{{ block.twitterHandle }}
                                    {% endif %}
                                     {{ event.startDate|date('m/d/y') }} @ {{ event.startDate|date('g:ia') }} 
                                     
                                     {{ block.bio }}
                                

                               {% set rows = rows + 1 %}

                        {% endswitch %}
                    {% endfor %}
            {% endfor %}
        {% endif %}
    {% endfor %}


It's important to set the variable outside the for loop, otherwise it does not work.

> **Comments:**
> **username_0**: Thank you for answering. As with every variation I've tried on this solution, I tend to get this error: Impossible to access an attribute ("type") on a string variable ("146")  
> **username_0**: Found the issue! You put me on the right track. I like this solution and its totally different than any of the solutions I've seen for this problem. THANK YOU.  

---

