Conditional variables within a certain matrix block type 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" %}&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 %}


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.
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' %}
            &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 %}