Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:36:35.510Z
Answers.Text: 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.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:39:45.090Z
Answers.Text: Ah I see. So you could have several consecutive 'event' blocks in a row?
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:41:37.500Z
Answers.Text: 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.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:47:34.190Z
Answers.Text: Ok - working on a fix :)
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:48:00.000Z
Answers.Text: @AdamMcCombs Something like this then? http://craftcms.stackexchange.com/a/6739/9
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:51:16.747Z
Answers.Text: @BrandonKelly yes! that's it.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:53:20.260Z
Answers.Text: Nice! All set @AdamMcCombs?
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:53:55.043Z
Answers.Text: Yep @aberkie this is exactly what I needed!
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:34:58.950Z
Answers.LastActivityDate: 2015-02-20T22:34:58.950Z
Answers.UserDisplayName: Aaron Berkowitz
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:59:07.927Z
Answers.LastActivityDate: 2015-02-20T22:59:07.927Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-21T00:27:06.700Z
Answers.Text: Would have been nice if you had posted the code adapted to match you question, Adam.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T22:53:25.197Z
Answers.LastActivityDate: 2015-02-20T22:53:25.197Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Adam McCombs
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:27:32.527Z
Comments.Text: Is the end goal to one table for each event, or one big table listing all events?
Comments.UserDisplayName: Aaron Berkowitz
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:32:34.630Z
Comments.Text: The end output should be <h2>Day</h2><table><tr>event name + time</tr></table>
Comments.UserDisplayName: Adam McCombs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:33:52.370Z
Comments.Text: 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.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:37:31.123Z
Comments.Text: @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.
Comments.UserDisplayName: Adam McCombs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:41:08.367Z
Comments.Text: 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.
Comments.UserDisplayName: Douglas McDonald
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T22:43:53.340Z
Comments.Text: 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.
Comments.UserDisplayName: Adam McCombs
Tags: <table><switch><case>
Title: Opening and closing table within a switch
ThreadText: # 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' %}
              <h2>{{ block.day.format('l - F j, Y') }} </h2>
  <table><tbody>
  
  {% case 'event' %}
              <tr><td>{{ block.eventTime }}</td>
              <td>{{ block.eventName }}</td></tr>
  
  {% endswitch %}
          </tbody>
  </table>
  {% 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**: @username_2 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' %}
      <h2>{{ block.day.format('l - F j, Y') }} </h2>    
  {% case 'event' %}
      <table>
          <tbody>
              <tr><td>{{ block.eventTime }}</td>
              <td>{{ block.eventName }}</td></tr>
          </tbody>
      </table>    
  {% 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**: @username_0 Something like this then? http://craftcms.stackexchange.com/a/6739/9  
  > **username_0**: @username_3 yes! that's it.  
  > **username_1**: Nice! All set @username_0?  
  > **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') }}
      <table>
          <tbody>
              {% for block in blocks %}
                  <tr>
                      <td>{{ block.eventTime }}</td>
                      <td>{{ block.eventName }}</td>
                  </tr>
              {% endfor %}
          </tbody>
       </table>
  {% endfor %}
  
  ```
  
  Not tested.
  
  
  ---
  
  ### Answer by username_0 on 2015-02-20. Score: 1
  
  username_3 sent over another post that answer this exact problem: [Solution for Matrix-within-Matrix (or More Robust Table Fields)](https://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields/6739#6739)
  
  ```
  {% 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 %}
              <div class="stats">
          {% endif %}
  
          <!-- Stat block HTML -->
  
          {% if isLastConsecutiveBlockOfType %}
              </div>
          {% endif %}
  
      {% case 'quote' %}
  
          {% if isFirstConsecutiveBlockOfType %}
              <div class="quotes">
          {% endif %}
  
          <!-- Quote block HTML -->
  
          {% if isLastConsecutiveBlockOfType %}
              </div>
          {% endif %}
  
  {% endswitch %}{% endfor %}
  
  ```
  
  > **Comments:**
  > **username_4**: Would have been nice if you had posted the code adapted to match you question, username_0.  
  
  ---
  
  