<p>My answer is based on the assumption you only have one matrix module you want to compare, otherwise you would have to store your values in an array with the key as <code>block.getType().handle</code> or you would have to create multiple variables.</p>

<p>You want to create a variable that stores the value of your field if the block handle matches. So you check if your handles are equal and compare them</p>

<pre><code>{% set latestBlockValue = null %}
{% for block in entry.matrix %}
    {% if block.getType().handle == 'referenceBlockType' %}
        {% if latestBlockValue is not null and latestBlockValue == x %}
            {# z #}
        {% endif %}
        {% set latestBlockValue = block.y %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-11-09T19:53:12.467Z
Thanks for the reply! So I think I'm a bit confused by the assumption here. Are you saying that you're assuming that there is only one matrix block that contains the above code? Or that there could be multiple matrix blocks containing the above code but that there is only one other block containing the field for which the code is doing the comparison?
dpayne
CC BY-SA 3.0
2017-11-09T19:54:50.397Z
I assume you have only one block type you want to compare.
Robin Schambach
CC BY-SA 3.0
2017-11-09T21:46:19.853Z
Ok got it. Is the other implicit assumption that the data in the field handle would always be the same? Maybe I'm getting this mixed up in my mind but if you have different data in the field handle each time then the condition would never be true because your latest block value would never equal your updated variable on the next loop.
dpayne
CC BY-SA 3.0
2017-11-09T21:52:22.193Z
Right now I check if the values are the same but you can change the code however you want. You did not specify your condition so I just created an example
Robin Schambach
CC BY-SA 3.0
2017-11-10T03:49:21.753Z
Man, I feel like an idiot but I've been banging my head against the wall trying to modify the above / try different approaches to accomplish what I'm looking to do in the question but just haven't been able to make it work.

I found a way to accomplish my highest level goal using a completely different approach outside of the context of this question but am still curious how to achieve what I asked in the question. I updated the above to make it more specific to what I originally sought to do.

Apologies for not having more specifity in the original question.
dpayne
CC BY-SA 3.0
2017-11-10T05:29:12.603Z
Should the `referenceBlockType` and the block with the if statement be the same type? If yes - that's exactly what my code does. It checks if the previous block with the same type has a certain value in a specific field
Robin Schambach
CC BY-SA 3.0
2017-11-10T15:44:38.047Z
The referenceBlockType and the block(s) with the if statement would be different types. The blocks that contain the if statement could all be different types from one another, but the consistent element amongst them is they would all be different from referenceBlockType.
dpayne
CC BY-SA 3.0
2017-11-09T06:17:08.253Z
2017-11-10T05:25:11.627Z
2017-11-10T05:25:11.627Z
Robin Schambach
<p>I’m looping through blocks in a matrix field on a page. In multiple different blocks in that loop, all of which could be different block types, I’d like to have an if statement that basically says if the most recent instance of a particular block type (we'll call it "referenceBlockType") contains x in y field, then do z. Note that by most recent instance I mean the closest previous instance of referenceBlockType relative to the position of the block that contains the if statement.</p>

<p>referenceBlockType appears multiple times in the loop and is not necessarily a direct sibling of the block that is referencing it, nor is it always the first or last field in the loop.</p>

<p>How would I do that? Thanks! </p>

<matrix>
If Most Recent Instance of a Block Contains X in Y Field Then Do Z
# If Most Recent Instance of a Block Contains X in Y Field Then Do Z

(Asked by: username_0 on 2017-11-09)

I’m looping through blocks in a matrix field on a page. In multiple different blocks in that loop, all of which could be different block types, I’d like to have an if statement that basically says if the most recent instance of a particular block type (we'll call it "referenceBlockType") contains x in y field, then do z. Note that by most recent instance I mean the closest previous instance of referenceBlockType relative to the position of the block that contains the if statement.

referenceBlockType appears multiple times in the loop and is not necessarily a direct sibling of the block that is referencing it, nor is it always the first or last field in the loop.

How would I do that? Thanks!

**Tags:** matrix


---

## 1 answers

### Answer by username_1 on 2017-11-09. Score: 1

My answer is based on the assumption you only have one matrix module you want to compare, otherwise you would have to store your values in an array with the key as block.getType().handle or you would have to create multiple variables.
You want to create a variable that stores the value of your field if the block handle matches. So you check if your handles are equal and compare them
{% set latestBlockValue = null %}
{% for block in entry.matrix %}
    {% if block.getType().handle == 'referenceBlockType' %}
        {% if latestBlockValue is not null and latestBlockValue == x %}
            {# z #}
        {% endif %}
        {% set latestBlockValue = block.y %}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks for the reply! So I think I'm a bit confused by the assumption here. Are you saying that you're assuming that there is only one matrix block that contains the above code? Or that there could be multiple matrix blocks containing the above code but that there is only one other block containing the field for which the code is doing the comparison?  
> **username_1**: I assume you have only one block type you want to compare.  
> **username_0**: Ok got it. Is the other implicit assumption that the data in the field handle would always be the same? Maybe I'm getting this mixed up in my mind but if you have different data in the field handle each time then the condition would never be true because your latest block value would never equal your updated variable on the next loop.  
> **username_1**: Right now I check if the values are the same but you can change the code however you want. You did not specify your condition so I just created an example  
> **username_0**: Man, I feel like an idiot but I've been banging my head against the wall trying to modify the above / try different approaches to accomplish what I'm looking to do in the question but just haven't been able to make it work.  I found a way to accomplish my highest level goal using a completely different approach outside of the context of this question but am still curious how to achieve what I asked in the question. I updated the above to make it more specific to what I originally sought to do.  Apologies for not having more specifity in the original question.  
> **username_1**: Should the `referenceBlockType` and the block with the if statement be the same type? If yes - that's exactly what my code does. It checks if the previous block with the same type has a certain value in a specific field  
> **username_0**: The referenceBlockType and the block(s) with the if statement would be different types. The blocks that contain the if statement could all be different types from one another, but the consistent element amongst them is they would all be different from referenceBlockType.  

---

