Answers.Body: <p>Why don't you do something like this? You can use switch and case to change the output of each part of the matrix (we call them blocks).</p>
  
  <p>This is part of a snippet to output a documentation matrix that contains code, titles, content etc.</p>
  
  <pre><code>{% for block in entry.documentation %}
      {% switch block.type %}
  
           {% case 'subsectionTitle' %}
              &lt;h2 id="{{ anchor }}"&gt;{{ block.subsectionTitle }} &lt;a href="#{{ anchor }}" class="pound"&gt;#&lt;/a&gt;&lt;/h2&gt;
  
          {% case 'description' %}
               &lt;div class="docs-description"&gt;{{ block.description }}&lt;/div&gt;
  
      {% endswitch %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-23T21:04:38.930Z
Answers.Text: This won't solve the problem of the opening and closing tags of the parent div `row`. So the first type opens the `<div class="row">` and the last of type closes the parent row `</div>`. I may change the block so everything is contained within the scope (see my comment below the post)
Answers.UserDisplayName: Siebird
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-09-23T20:59:13.677Z
Answers.LastActivityDate: 2019-04-01T17:33:08.517Z
Answers.LastEditDate: 2019-04-01T17:33:08.517Z
Answers.UserDisplayName: Adam McCombs
Body: <p>So I have a matrix field with 3 different block types. The first two block types have markup contained within their scope. The last type, has opening and closing tags for the first of type and last of type (<em>basically creating a row with two columns</em>). I was able to successfully get the markup, but wanted to see if there is a better way to to this write this. </p>
  
  <pre><code>{% for block in entry.homeContentBlocks %}
      {% if block.type == "leadGeneration" %}
          ...
      {% elseif block.type == "banner" %}
          ...
      {% else %}
          {% if block.next() %}
              {% if block.next().type == "contentBlock" %}&lt;div class="row"&gt;{% endif %}
          {% endif %}
  
              {% if block.next() %}
                  {% if block.next().type == "contentBlock" %}
                      &lt;div class="block-6 v-divider"&gt;
                  {% endif %}
              {% endif %}
              {% if block.prev() %}
                  {% if block.prev().type == "contentBlock" %}
                      &lt;div class="block-6 pad-left"&gt;
                  {% endif %}
              {% endif %}
  
                  &lt;h3&gt;{{ block.heading }}&lt;/h3&gt;
                  {{ block.body }}
                  {% if block.btnLink | length %}
                      {% set link = block.btnLink.first().url %}
                      &lt;a href="{{ link }}" class="btn"&gt;{{ block.btnLabel }}&lt;/a&gt;
                  {% endif %}
              &lt;/div&gt;
  
          {% if block.prev() %}
              {% if block.prev().type == "contentBlock" %}&lt;/div&gt;{% endif %}
          {% endif %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>So this works, but let's say the blocks out of sequence. I thought maybe a increment variable could work, but wasn't sure how to implement it. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-09-23T20:43:22.897Z
Comments.Text: I wonder if I should've setup the block type to have both columns info contained within one block on the admin side. This would simplify the template logic and most likely minimize data entry error. Hmmmm...
Comments.UserDisplayName: Siebird
Tags: <matrix><conditional>
Title: Conditional variables within a certain matrix block type
ThreadText: # Conditional variables within a certain matrix block type
  
  (Asked by: username_0 on 2014-09-23)
  
  So I have a matrix field with 3 different block types. The first two block types have markup contained within their scope. The last type, has opening and closing tags for the first of type and last of type (*basically creating a row with two columns*). I was able to successfully get the markup, but wanted to see if there is a better way to to this write this.
  
  ```
  {% for block in entry.homeContentBlocks %}
      {% if block.type == "leadGeneration" %}
          ...
      {% elseif block.type == "banner" %}
          ...
      {% else %}
          {% if block.next() %}
              {% if block.next().type == "contentBlock" %}<div class="row">{% endif %}
          {% endif %}
  
              {% if block.next() %}
                  {% if block.next().type == "contentBlock" %}
                      <div class="block-6 v-divider">
                  {% endif %}
              {% endif %}
              {% if block.prev() %}
                  {% if block.prev().type == "contentBlock" %}
                      <div class="block-6 pad-left">
                  {% endif %}
              {% endif %}
  
                  <h3>{{ block.heading }}</h3>
                  {{ block.body }}
                  {% if block.btnLink | length %}
                      {% set link = block.btnLink.first().url %}
                      <a href="{{ link }}" class="btn">{{ block.btnLabel }}</a>
                  {% endif %}
              </div>
  
          {% if block.prev() %}
              {% if block.prev().type == "contentBlock" %}</div>{% endif %}
          {% endif %}
      {% endif %}
  {% endfor %}
  
  ```
  
  So this works, but let's say the blocks out of sequence. I thought maybe a increment variable could work, but wasn't sure how to implement it.
  
  **Tags:** matrix, conditional
  
  > **Comments:**
  > **username_0**: I wonder if I should've setup the block type to have both columns info contained within one block on the admin side. This would simplify the template logic and most likely minimize data entry error. Hmmmm...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-09-23. Score: 1
  
  Why don't you do something like this? You can use switch and case to change the output of each part of the matrix (we call them blocks).
  
  This is part of a snippet to output a documentation matrix that contains code, titles, content etc.
  
  ```
  {% for block in entry.documentation %}
      {% switch block.type %}
  
           {% case 'subsectionTitle' %}
              <h2 id="{{ anchor }}">{{ block.subsectionTitle }} <a href="#{{ anchor }}" class="pound">#</a></h2>
  
          {% case 'description' %}
               <div class="docs-description">{{ block.description }}</div>
  
      {% endswitch %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: This won't solve the problem of the opening and closing tags of the parent div `row`. So the first type opens the `<div class="row">` and the last of type closes the parent row `</div>`. I may change the block so everything is contained within the scope (see my comment below the post)  
  
  ---
  
  