Answers.Body: <p>A possible solution could be this:
  Instead of outputting your blockType3 immediately, save all blockType3 ID's in an array. When your loop has finished (check for loop.last) output all the blocktypes from the array at once.</p>
  
  <p>This code is just as an example (untested):</p>
  
  <pre><code>{% set arrBlockType3 = [] %}
  {% for block in entry.matrixField.all() %}
      {% switch block.type %}
          {% case 'blockType1' %}
              &lt;div&gt;
                  {{ block.field }}
              &lt;/div&gt;
              {% case 'blockType2' %}
              &lt;div&gt;
                  {{ block.field }}
              &lt;/div&gt;
              {% case 'blockType3' %}
                  {% set arrBlockType3 = arrBlockType3 | merge([block.id]) %}
      {% endswitch %}
      {% if loop.last %}
          &lt;ul&gt;
          {% for id in arrBlockType3 %}
              {% set block = entry.matrixField.id(id).first() %}
              &lt;li&gt;{{ block.field }}&lt;/li&gt;
          {% endfor %}
          &lt;/ul&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T07:50:55.067Z
Answers.LastActivityDate: 2018-09-28T07:50:55.067Z
Answers.UserDisplayName: Tom De Smet
Body: <p>I made a template including a matrix field. I want to wrap all occurrences of a certain block type into a <code>&lt;ul&gt;</code> element. How can I do this?</p>
  
  <p>My current code:</p>
  
  <pre><code> {% for block in entry.matrixField.all() %}
          {% switch block.type %}
              {% case 'blockType1' %}
              &lt;div&gt;
                  {{ block.field }}
              &lt;/div&gt;
              {% case 'blockType2' %}
              &lt;div&gt;
                  {{ block.field }}
              &lt;/div&gt;
              {% case 'blockType3' %}
              &lt;ul&gt;
                  &lt;li&gt;
                      {{ block.field }}
                  &lt;/li&gt;
              &lt;/ul&gt;
          {% endswitch %}
      {% endfor %}
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>But I want my output like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
      &lt;li&gt;blockfield&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>That is, I want the <code>ul</code> tag to follow the loop of blockType3.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-28T08:20:24.443Z
Comments.Text: This question has some tips on how to dynamically wrap block types that are of the same type if they’re next to each other. https://craftcms.stackexchange.com/q/1745/50
Comments.UserDisplayName: Simon Kuran
Tags: <templating><matrix>
Title: How to wrap a matrix blocktype occurrence loop inside a ul?
ThreadText: # How to wrap a matrix blocktype occurrence loop inside a ul?
  
  (Asked by: username_0 on 2018-09-24)
  
  I made a template including a matrix field. I want to wrap all occurrences of a certain block type into a `<ul>` element. How can I do this?
  
  My current code:
  
  ```
   {% for block in entry.matrixField.all() %}
          {% switch block.type %}
              {% case 'blockType1' %}
              <div>
                  {{ block.field }}
              </div>
              {% case 'blockType2' %}
              <div>
                  {{ block.field }}
              </div>
              {% case 'blockType3' %}
              <ul>
                  <li>
                      {{ block.field }}
                  </li>
              </ul>
          {% endswitch %}
      {% endfor %}
  
  ```
  
  Current output:
  
  ```
  <ul>
      <li>blockfield</li>
  </ul>
  <ul>
      <li>blockfield</li>
  </ul>
  <ul>
      <li>blockfield</li>
  </ul>
  
  ```
  
  But I want my output like this:
  
  ```
  <ul>
      <li>blockfield</li>
      <li>blockfield</li>
      <li>blockfield</li>
  </ul>
  
  ```
  
  That is, I want the `ul` tag to follow the loop of blockType3.
  
  **Tags:** templating, matrix
  
  > **Comments:**
  > **username_1**: This question has some tips on how to dynamically wrap block types that are of the same type if they’re next to each other. https://craftcms.stackexchange.com/q/1745/50  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-09-28. Score: 2
  
  A possible solution could be this:
  Instead of outputting your blockType3 immediately, save all blockType3 ID's in an array. When your loop has finished (check for loop.last) output all the blocktypes from the array at once.
  
  This code is just as an example (untested):
  
  ```
  {% set arrBlockType3 = [] %}
  {% for block in entry.matrixField.all() %}
      {% switch block.type %}
          {% case 'blockType1' %}
              <div>
                  {{ block.field }}
              </div>
              {% case 'blockType2' %}
              <div>
                  {{ block.field }}
              </div>
              {% case 'blockType3' %}
                  {% set arrBlockType3 = arrBlockType3 | merge([block.id]) %}
      {% endswitch %}
      {% if loop.last %}
          <ul>
          {% for id in arrBlockType3 %}
              {% set block = entry.matrixField.id(id).first() %}
              <li>{{ block.field }}</li>
          {% endfor %}
          </ul>
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  