<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>

CC BY-SA 3.0
2014-09-23T21:04:38.930Z
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)
Siebird
CC BY-SA 4.0
2014-09-23T20:59:13.677Z
2019-04-01T17:33:08.517Z
2019-04-01T17:33:08.517Z
Adam McCombs
<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>

CC BY-SA 3.0
2014-09-23T20:43:22.897Z
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...
Siebird
<matrix><conditional>
Conditional variables within a certain matrix block type
# 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" %}{% endif %}
        {% endif %}

            {% if block.next() %}
                {% if block.next().type == "contentBlock" %}
                    
                {% endif %}
            {% endif %}
            {% if block.prev() %}
                {% if block.prev().type == "contentBlock" %}
                    
                {% endif %}
            {% endif %}

                {{ block.heading }}
                {{ block.body }}
                {% if block.btnLink | length %}
                    {% set link = block.btnLink.first().url %}
                    {{ block.btnLabel }}
                {% endif %}
            

        {% if block.prev() %}
            {% if block.prev().type == "contentBlock" %}{% 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' %}
            {{ block.subsectionTitle }} #

        {% case 'description' %}
             {{ block.description }}

    {% 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)  

---

