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!


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 %}
  • 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
    Nov 9 '17 at 19:53
  • I assume you have only one block type you want to compare. Nov 9 '17 at 19:54
  • 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
    Nov 9 '17 at 21:46
  • 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 Nov 9 '17 at 21:52
  • 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
    Nov 10 '17 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.