<p>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.</p>

<p>You can find a great working example on the happy lager demo website (<a href="https://github.com/pixelandtonic/HappyLager" rel="nofollow">https://github.com/pixelandtonic/HappyLager</a>). </p>

<p>The relevant code part is here: <a href="https://github.com/pixelandtonic/HappyLager/blob/master/craft/templates/_includes/article_body.html" rel="nofollow">https://github.com/pixelandtonic/HappyLager/blob/master/craft/templates/_includes/article_body.html</a>.</p>

CC BY-SA 3.0
2016-02-23T18:08:40.103Z
That's a great answer Tom!
Matt P
CC BY-SA 3.0
2016-02-24T09:00:01.223Z
It got me in the right direction Tom. I already had tried placing it outside the block loop, but made an error in the syntax.
noregt
CC BY-SA 3.0
2016-02-23T17:55:22.363Z
2016-02-23T17:55:22.363Z
Tom Bauer
<p>In addition to Tom's excellent answer, you could also tackle this with a simple if-loop-index condition. <a href="https://craftcms.stackexchange.com/questions/1863/return-matrix-field-block-number">Have a look at this answer</a></p>

CC BY-SA 3.0
2016-02-23T18:20:30.227Z
2016-02-23T18:20:30.227Z
2017-04-13T12:50:59.930Z
Matt P
<p>Tom's answer got me in the right direction. I ended up using this:</p>

<pre><code>{% for block in entry.detailMatrix %}
   {# If there is a block type images, but no previous block images, start the div #}
   {% if block.type == 'images' and block.prev().type != 'images'%}
        &lt;div class="section" &gt;
    {% endif %}

    {% if block.type=="images" %}
        {{block.image}}
    {% endif %}

    {# If there is a block type images, but no next block images, close the div. Place in the bottom if there are more block types #}
    {% if block.type == 'images' and block.next().type != 'images'%}
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Instead of using "not" using != worked better</p>

CC BY-SA 3.0
2016-02-24T09:04:18.460Z
2016-02-24T09:04:18.460Z
noregt
<p>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. </p>

<p>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:</p>

<pre><code>{% 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 %}
</code></pre>

<matrix><loop>
Parse html on first and last occurrence of a matrix block type in a series of that same block type
# Parse html on first and last occurrence of a matrix block type in a series of that same block type

(Asked by: username_0 on 2016-02-23)

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'%}
            
        {% endif %}
            {{block.image}}
            
        {% if block.prev().type == 'images' and not block.next().type == 'images'%}
            
        {% endif %}
    {% endif %}
{% endfor %}

**Tags:** matrix, loop

---

## 3 answers

### Answer by username_1 on 2016-02-23. Score: 3 | Accepted answer

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

> **Comments:**
> **username_2**: That's a great answer Tom!  
> **username_0**: It got me in the right direction Tom. I already had tried placing it outside the block loop, but made an error in the syntax.  

---

### Answer by username_2 on 2016-02-23. Score: 1

In addition to Tom's excellent answer, you could also tackle this with a simple if-loop-index condition. [Have a look at this answer](https://craftcms.stackexchange.com/questions/1863/return-matrix-field-block-number)


---

### Answer by username_0 on 2016-02-24. Score: 1

Tom's answer got me in the right direction. I ended up using this:
{% for block in entry.detailMatrix %}
   {# If there is a block type images, but no previous block images, start the div #}
   {% if block.type == 'images' and block.prev().type != 'images'%}
        
    {% endif %}

    {% if block.type=="images" %}
        {{block.image}}
    {% endif %}

    {# If there is a block type images, but no next block images, close the div. Place in the bottom if there are more block types #}
    {% if block.type == 'images' and block.next().type != 'images'%}
        
    {% endif %}
{% endfor %}

Instead of using "not" using != worked better


---

