Skip to main content

Conditional Logic & Loops

Templates can be pretty powerful. They give you the ability to make your site dynamic, allowing what is displayed to change based upon user interaction, content, and other factors.

Conditional Logic

There may be instances where you want to display a block of content within a template only if certain conditions are met or not met. This is where <r:if> and <r:if_not> come in handy. These tags work by taking two inputs and comparing them using a given operator. If the comparison is true, <r:if> will render its block of content. If you would like to display the block of content when the comparison is false, you would use <r:if_not>.

CleanSlate Theme Development Tutorial Series
Video #24 - If/conditional statements

For example, let's say you want to display a block of content only if the name of the current page is named "Special".

<r:if value1="{page:name}" value2="Special" operator="=">
  <strong>This is a special page!!!</strong>

The same thing could be done with a little less typing:

<r:if v1="{page:name}" v2="Special" op="=">
  <strong>This is a special page!!!</strong>

Now suppose we only want to show a piece of content between a very specific time frame each day:

<r:if v1="now" v2="8:00 AM" op=">" type="Date">
  <r:if v1="now" v2="5:00 PM" op="<" type="Date">
     <strong>Displayed between 8 AM and 5 PM daily.</strong>

Notice that, in the example above, we set type="Date" in the <r:if> tags. This tells the system to treat the values being compared as dates instead of strings, which is the default. If the values should be treated as numbers, you would need to specify type="Number".

Also keep in mind, for the time based example above, that the system may cache page content for a short time, typically around 10-15 minutes, so you may not see the expected result immediately.

NOTE: Variable names in <r:if> statements must not contain minus (-) characters. Replace minus characters with underscores ( _) or camelCase variable names.

Read the tag documentation for more information.


CleanSlate Theme Development Tutorial Series
Video #20 - Loops in CleanSlate

If you have a block of content that you need to repeat several times, <r:loop> can be used instead of manually copying & pasting repeatedly. You can loop over a list of items, given by a delimited string, a range of integers, or a set number of iterations. The items to be looped over can come from various places, such as page content, page attributes, custom page attributes, or even the URL.

<r:loop items="foo,bar,baz">
<p>Item Count: <r:item_count /></p>
    <p><r:item /></p>

Read the tag documentation for more information.

Last updated on April 14, 2020.

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 for help or use the form on the request help page.