Matrix loop (nesting?) issue using switch First let me say this: I'm new to Craft and Twig. So: my apologies for probably overlooking the obvious. I hope you can offer me some help... I've been trying for a couple of days to solve this puzzle. Nothing seems to work. ;-S

I'm trying to create a simple events calendar using Craft 3, Twig and Bootstrap 4. The calendar exist of two parts: 1. a section with 3 columns (col-4) that lists the events; and 2. a divider (col-12) that contains the month and year. I used the Matrix field in Craft to be able to be flexible in sorting events within a certain period of time. 

The Issue:
I expected to see the three columns to neatly sit beside each other horizontally (on the desktop version of the site). But this doesn't seem to work right now: because I use a for loop to enable switch, the for loop I need for outputting the event-items results in the same row of events being repeated a couple of times instead of just once. 

What am I doing wrong? 

I very much appreciate any thoughts. 

Greetings from The Netherlands, sorry for my bad English,

Mars

{% block calendar %}

    {% for block in entry.calendarGroup.all() %}

        {% switch block.type %}

            {% case "events" %}

                &lt;div class="row no-gutters"&gt;

                    {% for entries in entry.calendarGroup.type('events') %}

                        &lt;div class="col-md-4 pt-1 pb-1 pb-md-0 pr-md-1"&gt;
                            &lt;div class="box-calendar black text-white"&gt;
                                &lt;div class="row no-gutters p-3"&gt;
                                    &lt;div class="col-2 pl-lg-1 pt-1"&gt;
                                        &lt;h5&gt;{{ block.eventDate }}&lt;/h5&gt;
                                    &lt;/div&gt;

                                    &lt;div class="col-10 pt-1 pr-2"&gt;
                                        &lt;h5&gt;{{ block.eventTitle }}&lt;/h5&gt;
                                        &lt;ul class="pt-1 mt-1 calendar-brood"&gt;
                                            &lt;li class="uppercase"&gt;{{ block.eventType }}&lt;/li&gt;
                                        &lt;/ul&gt;

                                        &lt;ul class="pt-3 mt-1 calendar-brood"&gt;
                                            &lt;li&gt;{{ block.eventTime }}&lt;/li&gt;
                                            &lt;li&gt;{{ block.eventLocation }}&lt;/li&gt;
                                        &lt;/ul&gt;
                                        &lt;div class="pt-3 mt-1 pb-3 calendar-brood"&gt;{{ block.eventDescription }}&lt;/div&gt;
                                    &lt;/div&gt;

                                &lt;/div&gt; 
                            &lt;/div&gt; 
                        &lt;/div&gt;

                    {% endfor %}

                &lt;/div&gt;

            {% case "months" %}

                &lt;div class="row no-gutters"&gt;

                    &lt;div class="col-12"&gt;
                        &lt;div class="calendar-bar d-flex justify-content-between justify-content-md-center"&gt;
                            &lt;p class="d-flex align-self-center agenda-item left w-25"&gt;{{ block.month }}&lt;/p&gt;
                            &lt;p class="d-flex align-self-center agenda-item right"&gt;{{ block.year }}&lt;/p&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;

                &lt;/div&gt; &lt;!-- End row --&gt;

            {% endswitch %}
        {% endfor %}

    &lt;/div&gt;

{% endblock %}
I'm not entirely clear what your Matrix blocks/fields are setup like, so it's a little hard to tell what's going on here.

But I'm going to take a stab at it.

Your outer loop is looping through all the blocks in the calendarGroup Matrix field.

Then you're using a switch to determine which piece of code to execute based on which one of the two block types each block is.

All that looks ok.

But here is I think where your problem lies:

{% for entries in entry.calendarGroup.type('events') %}


{% endfor %}


This loop is starting fresh and looping over all the Matrix blocks in the calendarGroup Matrix field, except this time it's only getting the blocks of the event block type.

Then, inside that loop you are using the {{ block.fieldName }} variables again. But block is set in your parent loop. And nowhere are you referencing entries from your inner loop.

So there is a mismatch in your logic. I think you can simply do away with the inner loop, it's it's going to out put all of the event blocks inside each event block in the outer loop.  That, to me, seems like the issue.