ifcontent

The ifcontent tag allows you to conditionally output a content area or some fallback content. This lets you test to see if the content area has any content. If the content area does have content then you can display that content along with any other HTML or Twig code. If not, then some optional fallback content can be displayed with the optional {% else %} tag.

This tag is useful if you need to wrap the output from a content area in some HTML but only want that HTML to be used if the content area actually has content. 

Below is a simple example.

{% ifcontent 'contentAreaName' %}
     <div class="Content">{{ content }}</div>
{% else %}
     <div class="ContentAlt">Some alternate content here</div>
{% endifcontent %}

This replaces the use of the {{ _page.content() }} tag. Both of them would not be used for the same content area.

For example, instead of

{{ _page.content('sidebar') }}

you would do something like this:

{% ifcontent 'sidebar' %}
     <div class="Sidebar">{{ content }}</div>
{% endifcontent %}

Tag structure

The structure of the ifcontent tag is as follows:

{% ifcontent CONTENT_AREA_NAME %}
    {{ content }}
{% else % }
{% endifcontent %}

The opening tag "ifcontent" and the closing "endifcontent" should be all lowercase.

The CONTENT_AREA_NAME would typically be a string wrapped in single or double quotes like the examples above. It is required.

The {% else %} tag is completely optional. It should also be lowercase.

For example, if you only need to test for content so that you can wrap it in some specific HTML, then you can do something like the following:

{% ifcontent 'contentAreaName' %}
     <div class="Content">{{ content }}</div>
{% endifcontent %}

Displaying the content

By itself the ifcontent tag doesn't actually output the content for the specified content area. In order to output the content you need to use the special {{ content }} variable. That variable will hold the complete output of the specified content area.  It's only available within the "if" section of the tag and not within the "else" section.

{% ifcontent 'contentAreaName' %}
     {# The "content" variable is available here #}
     <div class="Content">{{ content }}</div>
{% else %}
     {# The content variable is NOT available within the else statement #}
     <p>No content here</p>
{% endifcontent %}

You also have access to any other variables that are in use in your template.

Using with other tags

You can use the {% ifcontent %} tag within a {% block %} tag.

{% block mainContent %}
   {% ifcontent 'main' %}
        <div class="Content">{{ content }}</div>
   {% else %}
        <div class="ContentAlt">Some alternate content here</div>
   {% endifcontent %}
{% endblock %}

You can also nest one {% ifcontent %} tag within another {% ifcontent %} tag.

{% block mainContent %}
   {% ifcontent 'main' %}
        <div class="Content">{{ content }}</div>
   {% else %}
           {% ifcontent 'otherContentarea' %}
               <div class="Content">{{ content }}</div>
           {% else %}
               <div class="ContentAlt">Some alternate content here</div>
           {% endifcontent %}
   {% endifcontent %}
{% endblock %}

You can also include any other Twig content in both the "if" section of the tag and/or the "else" section of the tag.

{% block mainContent %}
   {% ifcontent 'main' %}
        <div class="Content">{{ content }}</div>
        {# display blog posts here #}
        {{ _api.blog.posts.template('posts-api').limit(3).random() }}
   {% else %}
        {# Display calendar events here #}
        {{ _api.calendar.recentEvents.template('events-api').limit(3) }}
   {% endifcontent %}
{% endblock %}

< Back to the list of tags