Skip to main content

r:if and r:if_not

<r:if />

Renders a block of content only if a set of conditions are met or not met. <r:if> renders its content if the conditions are true. <r:if_not> renders its content if the conditions are false. The options are the same for both.

Attribute Options

value1 (v1) - The first value to be in the comparison. Its value can be hard coded or come from a page attribute, custom page attribute, variable set within your theme/template, a URL query parameter, or even a page content region.

value2 (v2) - The second value to be in the comparison. Its value can be hard coded or come from a page attribute, custom page attribute, variable set within your theme/template, a URL query parameter, or even a page content region.

operator (op) - The operator to use to compare value1 to value2. Can be one of: =, !=, >, <, >=, <=.

type - The type of value represented by value1 and value2. Can be one of: String (default), Number, Date, Boolean

Examples

<r:if value1="{page:name}" value2="Special" operator="=">
  <strong>This is a special page!!!</strong>
</r:if>
<r:if v1="{$some_variable}" v2="3.45" op=">" type="Number">
  Rendered if value1, which comes from a variable, is greater than 3.45, when treated as a Number.
</r:if>
<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>
  </r:if>
</r:if>

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

if, elsif, else, unless and case/when

These tags render a block of content only if a set of conditions are or are not met.

See our documentation on Conditional Logic and Loops for more in-depth explanations. Also, see the official Liquid documentation on Control Flow for more examples.

Attribute options

In Liquid, if tags don't have value attributes. They compare the actual items themselves.

NOTE: Variable names in if statements must not contain dash/minus (-) characters. Replace dashes with underscores (_) or camelCase variable names.

operator - The operator to use to compare value1 to value2. Can be one of the following: ==, != (does not equal), >, <, >=, <=.

or/and - Use these keywords to check two different conditions simultaneously.

contains/blank/empty - In addition to the standard operators, we can also use these aforementioned keywords. See our documentation on If Statement Expressions to learn more.

Examples

Check if a page name is equal to a specific string:

{% if page.name == "Special" %}
  <strong>This is a special page!!!</strong>
{% endif %}

Add an else to the above example to catch when the page name is not "Special":

{% if page.name == "Special" %}
  <strong>This is a special page!!!</strong>
{% else %}
  This is not a special page.
{% endif %}

Add an elsif to the example above:

{% if page.name == "Special" %}
  <strong>This is a special page!!!</strong>
{% elsif page.name == "Backpage 1" %}
  You won the lottery!
{% else %}
  This is not a special page.
{% endif %}

Combine checks using or:

{% if page.name == "Special" or page.name == "Backpage 1" %}
  <strong>This is a special page!!!</strong>
{% endif %}

Check if a variable contains a string:

{% assign bearVar = "Bear" %}
{% if bearVar contains "Bear" %}
  <p>Contains a Bear!</p>
{% endif %}

Check if the value of a variable is greater than 3.45:

{% assign some_variable = 4.25 %}
{% if some_variable > 3.45 %}
  <p>Rendered if <code>some_variable</code> is greater than 3.45.</p>
{% endif %}

Render content between a certain time of day:

{% assign now = 'now' | to_time %}
{% assign t1 = '8:00 AM' | to_time %}
{% assign t2 = '5:00 PM' | to_time %}
{% if now > t1 and now < t2 %}
  <strong>Displayed between 8 AM and 5 PM daily.</strong>
{% else %}

Check against multiple cases using case/when:

{% assign animal = "Bear" %}
{% case animal %}
  {% when "Wolf" %}
     This is a wolf.
  {% when "Bear" %}
     This is a Bear!
  {% else %}
     This is not a Wolf nor a Bear.
{% endcase %}

Last updated on March 15, 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.