Check Whether Blocks Contain Content

It can be useful to know whether the Blocks in a template contain any content. The Blocks content may be optional to begin with and knowing whether there is any allows you to conditionally handle things.

For instance, here is a use case where it makes sense to rely on the existense of Blocks content as an indication whether an item in a list should have a detail page and thus should include a “read more” link.

To do this, you can use the conditional tag perch:if with the exists attribute and test against _blocks:

<perch:if exists="_blocks">
<!--* your code *-->
<a href="/services/<perch:content id="slug">">Read more</a>

_blocks is a special value automatically added by Perch.


Related articles