Check Block Type Against Previous Block

It can be handy to check whether a Block type is different than the previous Block. You can perform the check with Perch conditional tag perch:if with the different attribute and test against _block_type:

<perch:block type="video" label="Video" icon="video">
    <perch:if different="_block_type">
        <!--* This block is different than the previous block *-->
    </perch:if>
</perch:block>

Where did _block_type come from?

The value is automatically added by Perch. If you use the perch:showall tag, you can inspect what’s available to you inside a block:

<perch:block type="video" label="Video" icon="video">
    <perch:template path="content/blocks/video.html">
    <perch:showall>
</perch:block>

What does the different attribute do?

The different attribute can be used with the perch:if tag. It compares a value of the current item against that of the previous item.

In this context we test against _block_type. You can think of each block as an item and collectively the blocks is a list of items. Each item has this special value _block_type.

First block

For the first block the condition different="_block_type" may evaluate to true. Depending on your requirements you may want to exclude the first block with another conditional tag:

<perch:block type="video" label="Video" icon="video">
    <perch:if different="_block_type">
        <!--* This block is different than the previous block *-->
        <perch:if not-exists="perch_item_first">
            <!--* This block is not the first item *-->
        </perch:if>
    </perch:if>
</perch:block>
link

Related articles