<p>I think the opening and closing tags in the table are in the wrong spot. Try opening and closing the table inside the event case.</p>

<pre><code>{% for block in entry.scheduleMatrix %}

{% switch block.type %}

{% case 'day' %}
    &lt;h2&gt;{{ block.day.format('l - F j, Y') }} &lt;/h2&gt;    
{% case 'event' %}
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;&lt;td&gt;{{ block.eventTime }}&lt;/td&gt;
            &lt;td&gt;{{ block.eventName }}&lt;/td&gt;&lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;    
{% endswitch %}

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

CC BY-SA 3.0
2015-02-20T22:36:35.510Z
Close but the event time and name loop over and over again. This ouputs table tr /table and I need something like table tr tr tr tr tr /table.
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:39:45.090Z
Ah I see. So you could have several consecutive 'event' blocks in a row?
Aaron Berkowitz
CC BY-SA 3.0
2015-02-20T22:41:37.500Z
Correct. That's why I'm thrown off. I don't know how to inject the opening and closing of the table between the cases.
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:47:34.190Z
Ok - working on a fix :)
Aaron Berkowitz
CC BY-SA 3.0
2015-02-20T22:48:00.000Z
@AdamMcCombs Something like this then? http://craftcms.stackexchange.com/a/6739/9
Brandon Kelly
CC BY-SA 3.0
2015-02-20T22:51:16.747Z
@BrandonKelly yes! that's it.
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:53:20.260Z
Nice! All set @AdamMcCombs?
Aaron Berkowitz
CC BY-SA 3.0
2015-02-20T22:53:55.043Z
Yep @aberkie this is exactly what I needed!
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:34:58.950Z
2015-02-20T22:34:58.950Z
Aaron Berkowitz
<p>An alternate solution would be to add a date field to each event block, you can then group blocks by date on output, and wouldn't have to worry about the client accidentally putting an event in the wrong order. Something like the following (although my syntax might be off a bit).</p>

<pre><code>{% set blocksByDate = entry.scheduleMatrix|group(day|date("Y-m-d") ) %}
{% for day, blocks in blocksByDay %}
    {{ day.format('l - F j, Y') }}
    &lt;table&gt;
        &lt;tbody&gt;
            {% for block in blocks %}
                &lt;tr&gt;
                    &lt;td&gt;{{ block.eventTime }}&lt;/td&gt;
                    &lt;td&gt;{{ block.eventName }}&lt;/td&gt;
                &lt;/tr&gt;
            {% endfor %}
        &lt;/tbody&gt;
     &lt;/table&gt;
{% endfor %}
</code></pre>

<p>Not tested.</p>

CC BY-SA 3.0
2015-02-20T22:59:07.927Z
2015-02-20T22:59:07.927Z
Douglas McDonald
<p>Brandon sent over another post that answer this exact problem: <a href="https://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields/6739#6739">Solution for Matrix-within-Matrix (or More Robust Table Fields)</a></p>

<pre><code>{% for block in entry.myMatrixField %}

{# Find out if this is the first/last consecutive block of this type #}
{% set type = block.type.handle %}
{% set isFirstConsecutiveBlockOfType = (loop.first or type != block.getPrev().type.handle) %}
{% set isLastConsecutiveBlockOfType = (loop.last or type != block.getNext().type.handle) %}

{% switch type %}

    {% case 'stat' %}

        {% if isFirstConsecutiveBlockOfType %}
            &lt;div class="stats"&gt;
        {% endif %}

        &lt;!-- Stat block HTML --&gt;

        {% if isLastConsecutiveBlockOfType %}
            &lt;/div&gt;
        {% endif %}

    {% case 'quote' %}

        {% if isFirstConsecutiveBlockOfType %}
            &lt;div class="quotes"&gt;
        {% endif %}

        &lt;!-- Quote block HTML --&gt;

        {% if isLastConsecutiveBlockOfType %}
            &lt;/div&gt;
        {% endif %}

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

CC BY-SA 3.0
2015-02-21T00:27:06.700Z
Would have been nice if you had posted the code adapted to match you question, Adam.
carlcs
CC BY-SA 3.0
2015-02-20T22:53:25.197Z
2015-02-20T22:53:25.197Z
2017-04-13T12:50:59.930Z
Adam McCombs
<p>Within my matrix I have two blocks: Day and Event. Day is just a date field while the event block has two text fields.</p>

<p>What's the best way to wrap the event data, which needs to show up as a table format within this loop?</p>

<p>This currently spills everything out in the correct order but the table doesn't open and close correctly around the event block.</p>

<p>The final output should look something like</p>

<p>Title (h2)</p>

<p>Table open<br>
TR<br>
TR<br>
TR<br>
Table close</p>

<pre><code>{% for block in entry.scheduleMatrix %}

{% switch block.type %}

{% case 'day' %}
            &lt;h2&gt;{{ block.day.format('l - F j, Y') }} &lt;/h2&gt;
&lt;table&gt;&lt;tbody&gt;

{% case 'event' %}
            &lt;tr&gt;&lt;td&gt;{{ block.eventTime }}&lt;/td&gt;
            &lt;td&gt;{{ block.eventName }}&lt;/td&gt;&lt;/tr&gt;

{% endswitch %}
        &lt;/tbody&gt;
&lt;/table&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-20T22:27:32.527Z
Is the end goal to one table for each event, or one big table listing all events?
Aaron Berkowitz
CC BY-SA 3.0
2015-02-20T22:32:34.630Z
The end output should be <h2>Day</h2><table><tr>event name + time</tr></table>
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:33:52.370Z
Just an idea, but it sounds like it might be better to just include the date field in the event block as well (even though it's a bit redundant). You can then use the date field to group the blocks for output. It should also help maintain data integrity, and will allow you to return events by date, for use elsewhere, etc.
Douglas McDonald
CC BY-SA 3.0
2015-02-20T22:37:31.123Z
@DouglasMcDonald I understand but this is a schedule where there are multiple events per day. The events will also have assets, links etc. so I need the ability to loop events over and over again.
Adam McCombs
CC BY-SA 3.0
2015-02-20T22:41:08.367Z
Yes. That's what I'm suggesting — multiple blocks, one event per block, with same date on each block. Then group by date, display the date once, and all the events in a table for that day.
Douglas McDonald
CC BY-SA 3.0
2015-02-20T22:43:53.340Z
I'm with you but that creates a ton of redundant content entry. If there are 30 events during a day you have to select the same date 30 different times. Plus Craft forces you to keep 15min increments of time. Currently eventTime is a text field.
Adam McCombs
<table><switch><case>
Opening and closing table within a switch
# Opening and closing table within a switch

(Asked by: username_0 on 2015-02-20)

Within my matrix I have two blocks: Day and Event. Day is just a date field while the event block has two text fields.
What's the best way to wrap the event data, which needs to show up as a table format within this loop?
This currently spills everything out in the correct order but the table doesn't open and close correctly around the event block.
The final output should look something like
Title (h2)
Table open
TR
TR
TR
Table close
{% for block in entry.scheduleMatrix %}

{% switch block.type %}

{% case 'day' %}
            {{ block.day.format('l - F j, Y') }} 


{% case 'event' %}
            {{ block.eventTime }}
            {{ block.eventName }}

{% endswitch %}
        

{% endfor %}

**Tags:** table, switch, case
> **Comments:**
> **username_1**: Is the end goal to one table for each event, or one big table listing all events?  
> **username_0**: The end output should be <h2>Day</h2><table><tr>event name + time</tr></table>  
> **username_2**: Just an idea, but it sounds like it might be better to just include the date field in the event block as well (even though it's a bit redundant). You can then use the date field to group the blocks for output. It should also help maintain data integrity, and will allow you to return events by date, for use elsewhere, etc.  
> **username_0**: @DouglasMcDonald I understand but this is a schedule where there are multiple events per day. The events will also have assets, links etc. so I need the ability to loop events over and over again.  
> **username_2**: Yes. That's what I'm suggesting — multiple blocks, one event per block, with same date on each block. Then group by date, display the date once, and all the events in a table for that day.  
> **username_0**: I'm with you but that creates a ton of redundant content entry. If there are 30 events during a day you have to select the same date 30 different times. Plus Craft forces you to keep 15min increments of time. Currently eventTime is a text field.  

---

## 3 answers

### Answer by username_1 on 2015-02-20. Score: 2

I think the opening and closing tags in the table are in the wrong spot. Try opening and closing the table inside the event case.
{% for block in entry.scheduleMatrix %}

{% switch block.type %}

{% case 'day' %}
    {{ block.day.format('l - F j, Y') }}     
{% case 'event' %}
    
        
            {{ block.eventTime }}
            {{ block.eventName }}
        
        
{% endswitch %}

{% endfor %}

> **Comments:**
> **username_0**: Close but the event time and name loop over and over again. This ouputs table tr /table and I need something like table tr tr tr tr tr /table.  
> **username_1**: Ah I see. So you could have several consecutive 'event' blocks in a row?  
> **username_0**: Correct. That's why I'm thrown off. I don't know how to inject the opening and closing of the table between the cases.  
> **username_1**: Ok - working on a fix :)  
> **username_3**: @AdamMcCombs Something like this then? http://craftcms.stackexchange.com/a/6739/9  
> **username_0**: @BrandonKelly yes! that's it.  
> **username_1**: Nice! All set @AdamMcCombs?  
> **username_0**: Yep @aberkie this is exactly what I needed!  

---

### Answer by username_2 on 2015-02-20. Score: 2

An alternate solution would be to add a date field to each event block, you can then group blocks by date on output, and wouldn't have to worry about the client accidentally putting an event in the wrong order. Something like the following (although my syntax might be off a bit).
{% set blocksByDate = entry.scheduleMatrix|group(day|date("Y-m-d") ) %}
{% for day, blocks in blocksByDay %}
    {{ day.format('l - F j, Y') }}
    
        
            {% for block in blocks %}
                
                    {{ block.eventTime }}
                    {{ block.eventName }}
                
            {% endfor %}
        
     
{% endfor %}

Not tested.


---

### Answer by username_0 on 2015-02-20. Score: 1

Brandon sent over another post that answer this exact problem: Solution for Matrix-within-Matrix (or More Robust Table Fields)
{% for block in entry.myMatrixField %}

{# Find out if this is the first/last consecutive block of this type #}
{% set type = block.type.handle %}
{% set isFirstConsecutiveBlockOfType = (loop.first or type != block.getPrev().type.handle) %}
{% set isLastConsecutiveBlockOfType = (loop.last or type != block.getNext().type.handle) %}

{% switch type %}

    {% case 'stat' %}

        {% if isFirstConsecutiveBlockOfType %}
            
        {% endif %}

        

        {% if isLastConsecutiveBlockOfType %}
            
        {% endif %}

    {% case 'quote' %}

        {% if isFirstConsecutiveBlockOfType %}
            
        {% endif %}

        

        {% if isLastConsecutiveBlockOfType %}
            
        {% endif %}

{% endswitch %}{% endfor %}

> **Comments:**
> **username_4**: Would have been nice if you had posted the code adapted to match you question, Adam.  

---

