<p>You can do that by using the <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel#next"><code>next</code></a> parameter...</p>

<pre><code>{% for block in entry.myMatrixField %}
    {% if block.next %}
        &lt;p&gt;Next block type is: {{ block.next.type }}&lt;/p&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>It's important to check <code>{% if block.next %}</code>, because the last block won't have a "next". If you were to try to call <code>block.next.type</code> where there is no "next", an error will be thrown.</p>

CC BY-SA 3.0
2014-08-09T23:00:43.247Z
2014-08-09T23:10:40.997Z
2014-08-09T23:10:40.997Z
Lindsey D
<p>Fun question! :)</p>

<p>You can use <code>getNext()</code> and <code>getPrev()</code> <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel#methods">Methods</a> on the MatrixBlockModel documentation page.</p>

<pre><code>{% for point in entry.keyPoints %}

    {# if this is the "typeToWrap" block type #}
    {% if point.type == 'typeToWrap' %}

        {# if there is a previous point to check #}
        {% if point.getPrev() %}

            {# if the previous point wasn't the "typeToWrap" type start a ul #}
            {% if point.getPrev().type != 'typeToWrap' %}
                &lt;ul&gt;
            {% endif %}

        {# if there isn't a previous point start a ul #}
        {% else %}
            &lt;ul&gt;
        {% endif %}

    {% endif %}

    {# output point #}

    {# if this is the "typeToWrap" block type #}
    {% if point.type == 'typeToWrap' %}

        {# if there is a next point to check #}
        {% if point.getNext() %}

            {# if the next point isn't the "typeToWrap" type end a ul #}
            {% if point.getNext().type != 'typeToWrap' %}
                &lt;/ul&gt;
            {% endif %}

        {# if there isn't another point end a ul #}
        {% else %}
            &lt;/ul&gt;
        {% endif %}

    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-01-12T09:51:21.420Z
I needed something similar, and I was only checking `.next` and not `.prev`, so that gave me a good clue. In my case the design called for mixing 3 different block types, 2 of which could be full width or half width. This isn't really answer to the question so I don't want to add lots of code here, but it may be useful to someone else: [gist](https://gist.github.com/plonko/e7e839a7f5df847fdb89).
I basically used a counter to check if the loop was at the first or last typetowrap block. Thanks @SimonKuran !
Sebastian Thomas
CC BY-SA 3.0
2014-08-09T23:42:13.667Z
Nice work @Simon! Very detailed answer. These are both good answers IMHO... Yours is more specific to the OP's exact situation, whereas mine is more general purpose.
Lindsey D
CC BY-SA 3.0
2014-08-10T00:01:57.397Z
@LindseyD Thanks! I agree, we both have good answers to have here.
Simon Kuran
CC BY-SA 3.0
2014-08-09T23:05:53.643Z
2014-08-09T23:13:26.030Z
2014-08-09T23:13:26.030Z
Simon Kuran
<p>I have a type of content that i use on the homepage of a site I am building, the code is shown here:</p>

<pre><code>&lt;ul&gt;
    {% for point in entry.keyPoints %}
        &lt;li&gt;
            &lt;div class="a-ico"&gt;
                &lt;i class="fa fa-{{ point.icon }}"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;div class="a-info"&gt;
                &lt;h4&gt;{{ point.heading }}&lt;/h4&gt;
                {{ point.text }}
            &lt;/div&gt;
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>This does exactly what i want it to do. It opens a UL and then loops through the different items which are LI's and output accordingly.</p>

<p>I want to use this same type of content on inner pages however as a matrix block TYPE rather than being the only type of content like on the homepage in the above example.</p>

<p>The problem then is that the FOR loop starts before i even know if this is the type of content i am dealing with so I can't appropriately wrap this type in a UL. </p>

<p>I can solve this by checking if it is the FIRST of TYPE and LAST of TYPE and outputting the opening and closing UL tags that way.</p>

<p>I then run into another problem that the first and last of this type are not necessarily adjacent to each other. If that is the case then all the other content TYPEs in the matrix between the FIRST and LAST of this TYPE will be inside of the UL as well.</p>

<p>My question then is how can i determine if the other matrix blocks that are immediately adjacent to this TYPE are of the same TYPE and if they are don't close the UL and if they are NOT then close the UL.</p>

<p>My code to determine what type is is just a simple FOR loop and then inside it a series of conditionals checking the TYPE and then outputting the appropriate content accordingly.</p>

<matrix><conditional>
Identify immediate siblings block type in matrix
# Identify immediate siblings block type in matrix

(Asked by: username_0 on 2014-08-09)

I have a type of content that i use on the homepage of a site I am building, the code is shown here:

    {% for point in entry.keyPoints %}
        
            
                
            
            
                {{ point.heading }}
                {{ point.text }}
            
        
    {% endfor %}


This does exactly what i want it to do. It opens a UL and then loops through the different items which are LI's and output accordingly.
I want to use this same type of content on inner pages however as a matrix block TYPE rather than being the only type of content like on the homepage in the above example.
The problem then is that the FOR loop starts before i even know if this is the type of content i am dealing with so I can't appropriately wrap this type in a UL. 
I can solve this by checking if it is the FIRST of TYPE and LAST of TYPE and outputting the opening and closing UL tags that way.
I then run into another problem that the first and last of this type are not necessarily adjacent to each other. If that is the case then all the other content TYPEs in the matrix between the FIRST and LAST of this TYPE will be inside of the UL as well.
My question then is how can i determine if the other matrix blocks that are immediately adjacent to this TYPE are of the same TYPE and if they are don't close the UL and if they are NOT then close the UL.
My code to determine what type is is just a simple FOR loop and then inside it a series of conditionals checking the TYPE and then outputting the appropriate content accordingly.

**Tags:** matrix, conditional


---

## 2 answers

### Answer by username_2 on 2014-08-09. Score: 8 | Accepted answer

Fun question! :)
You can use getNext() and getPrev() Methods on the MatrixBlockModel documentation page.
{% for point in entry.keyPoints %}

    {# if this is the "typeToWrap" block type #}
    {% if point.type == 'typeToWrap' %}

        {# if there is a previous point to check #}
        {% if point.getPrev() %}

            {# if the previous point wasn't the "typeToWrap" type start a ul #}
            {% if point.getPrev().type != 'typeToWrap' %}
                
            {% endif %}

        {# if there isn't a previous point start a ul #}
        {% else %}
            
        {% endif %}

    {% endif %}

    {# output point #}

    {# if this is the "typeToWrap" block type #}
    {% if point.type == 'typeToWrap' %}

        {# if there is a next point to check #}
        {% if point.getNext() %}

            {# if the next point isn't the "typeToWrap" type end a ul #}
            {% if point.getNext().type != 'typeToWrap' %}
                
            {% endif %}

        {# if there isn't another point end a ul #}
        {% else %}
            
        {% endif %}

    {% endif %}

{% endfor %}

> **Comments:**
> **username_1**: Nice work @Simon! Very detailed answer. These are both good answers IMHO... Yours is more specific to the OP's exact situation, whereas mine is more general purpose.  
> **username_2**: @LindseyD Thanks! I agree, we both have good answers to have here.  
> **username_3**: I needed something similar, and I was only checking `.next` and not `.prev`, so that gave me a good clue. In my case the design called for mixing 3 different block types, 2 of which could be full width or half width. This isn't really answer to the question so I don't want to add lots of code here, but it may be useful to someone else: [gist](https://gist.github.com/plonko/e7e839a7f5df847fdb89). I basically used a counter to check if the loop was at the first or last typetowrap block. Thanks @SimonKuran !  

---

### Answer by username_1 on 2014-08-09. Score: 7

You can do that by using the next parameter...
{% for block in entry.myMatrixField %}
    {% if block.next %}
        Next block type is: {{ block.next.type }}
    {% endif %}
{% endfor %}

It's important to check {% if block.next %}, because the last block won't have a "next". If you were to try to call block.next.type where there is no "next", an error will be thrown.


---

