Skip to main content

r:page:if_has_content_for and r:page:unless_has_content_for

<r:page:if_has_content_for /> allows you to check if an editable region has content.

<r:page:unless_has_content_for /> is the equivalent of “if_doesnt_have_content_for”.

NOTE: You can use this tag to wrap anything in the <r:page /> context. This may prove helpful when pulling content from other pages via <r:page:content name="region-name" />.

Attribute Options

  • region="region-name" - The name of the editable region you want to check for content.
  • always_show_in_edit_mode - Boolean. Should the stuff between these tags show in edit mode? Default value: false.

Example

A good example for this tag is to check if there’s content in a sidebar on a page. If there’s not, don’t output the markup for the sidebar and make your main content area go full width (provided the CSS allows it).

<r:page:if_has_content_for region="wvu-sidebar" always_show_in_edit_mode="true">
  <aside class="sidebar">
    <r:editable_region name="wvu-sidebar" />
  </aside>
</r:page:if_has_content_for>

Another example is for student profiles. A student always has a major; however, a student doesn’t always have a minor. Here’s how to show a minor if they have one:

<r:page:if_has_content_for region="wvu-profile__minor" always_show_in_edit_mode="true">
  <p>
    <strong>Minor: </strong> <r:editable_region name="wvu-profile__minor" type="simple">
      Enter the person's minor. If they don't have one, delete this text and it won't show up in production.
    </r:editable_region>
  </p>
</r:page:if_has_content_for>

And here’s how to only show the minor on the profile index page if they have one:

<r:children:each> <!-- Heads up! You may already have this r:children:each tag in your template. -->
  <r:page:if_has_content_for region="wvu-profile__minor">
    <p>
      <strong>Minor: </strong><r:page:content name="wvu-profile__minor" />
    </p>
  </r:page:if_has_content_for>
</r:children:each>

{% if edit_mode or page.content["my_region_name"] != blank %}

This tag allows you to check if an editable region has content.

You could similarly write a statement with unless to do the opposite.

Attribute options

You must specify the name of an editable region to check via page.content["my_region_name"] where my_region_name is the name of the editable region.

Examples

A good example for this tag is to check if there’s content in a sidebar on a page. If there’s not, don’t output the markup for the sidebar:

{% if edit_mode or page.content["wvu-sidebar"] != blank %}
  <aside class="sidebar">
    {% editable_region name: "wvu-sidebar" %}
  </aside>
{% endif %}

Another example is for student profiles. A student always has a major; however, a student doesn’t always have a minor. Here’s how to show a minor if they have one:

{% if edit_mode or page.content["wvu-profile__minor"] != blank %}
  <p>
    <strong>Minor: </strong>
    {% editable_region_block name: "wvu-profile__minor", type="simple" %}
      Enter the person's minor. If they don't have one, delete this text and it won't show up in production.
    {% endeditable_region_block %}
  </p>
{% endif %}

You could similarly use this in a loop. For example, in a profile index page, you could check if there is content in the wvu-profile__minor field:

{% comment %} First, we must get the ID of the current profile_index page, then assign it to a "pages" variable: {% endcomment %}
{% assign pages = site | get_page: page.id %}

{% comment %} Then we take the "pages" variable, get the children from that variable and assign it to a "profiles" variable which we iterate through: {% endcomment %}
{% assign profiles = pages.children %}

{% for profile in profiles.all %}
  <h2>{{ profile.content["wvu-profile__name"] }}</h2>
  {% if page.content["wvu-profile__minor"] != blank %}
    <p>{{ profile.content["wvu-profile__minor"] }}</p>
  {% endif %}
{% endfor %}

FAQ

Why put edit_mode before page.content["xyz"]?

Short answer: performance. It's faster to check for edit_mode first, then check if there's content in a specific editable region versus the other way around.

Last updated on March 16, 2021.

We welcome all questions, feedback and bug reports. If you're having an issue, we usually need the following information:

  • A brief description of the issue
  • A link to the page where you saw the issue
  • Screenshots that illustrate the problem - How do I take a screenshot?

Kindly email CleanSlate@mail.wvu.edu for help or use the form on the request help page.