Parse html on first and last occurrence of a matrix block type in a series of that same block type I'd like to wrap a series of block.type (images and description) in a div. So at the first iteration of this block type it needs to parse the start of the div. In the last iteration of this block type it needs to close the div. 

I tried something like this, if there is a next block from the same type but no previous block, start the div and visa versa:

{% for block in entry.detailMatrix %}
    {% if block.type=="images" %}
        {% if block.next().type == 'images' and not block.prev().type == 'images'%}
            &lt;div class="section" &gt;
        {% endif %}
            {{block.image}}
            &lt;/div&gt;
        {% if block.prev().type == 'images' and not block.next().type == 'images'%}
            &lt;/div&gt;
        {% endif %}
    {% endif %}
{% endfor %}
You need to define the block previous/next variables outside of your for loop. Also you should check for the last element in the loop.

You can find a great working example on the happy lager demo website (https://github.com/pixelandtonic/HappyLager). 

The relevant code part is here: https://github.com/pixelandtonic/HappyLager/blob/master/craft/templates/_includes/article_body.html.