If statement expressions
CleanSlate allows us to use expressions inside r:if and r:if_not tags. Expressions can be used to check:
- if a value is blank
- if a value is not blank
- if a value contains an exact string or variable
- Note:
containsmatches exact strings. If you tried matchingBearandbear, these are not the same strings because one of theb's is capitalized.
- Note:
What can the if statement expression values be?
The expression can evaluate the following:
- Custom Page Data
- Custom Site Data
- Variables
BLANK & NOT(BLANK()) code examples:
The following code examples will all use a custom site data value for their expression.
Check if my_site_data_name is blank:
<r:if expression="{{ BLANK(my_site_data_name) }}">
<p>This paragraph will show if <code>my_site_data_name</code> does NOT have content</p>
</r:if>
Check if my_site_data_name is not blank:
<r:if_not expression="{{ BLANK(my_site_data_name) }}">
<p>Will show if there is content in site data: <r:site:data name="my_site_data_name" /></p>
</r:if_not>
This could also be written inside the expression using r:if instead
of r:if_not:
<r:if expression="{{ NOT(BLANK(my_site_data_name)) }}">
<p>Will show if there is content in site data: <r:site:data name="my_site_data_name" /></p>
</r:if>
contains code examples:
The following code examples check variables (which can also be custom page/site data) to see if their value contains the value we are looking for.
To test the following expressions, paste these r:set_var tags above your expressions:
<r:set_var name="bearVar" value="Bear" /> <!-- For testing purposes only! -->
<r:set_var name="wolfVar" value="wolf" />
<r:set_var name="myVar2" value="Bear" />
Check if bearVar contains “Bear”:
<r:if expression="{{ contains(bearVar, 'Bear') }}">
<p>Contains a Bear.</p>
</r:if>
Check if two variables contain the same value:
<r:if expression="{{ contains(bearVar, myVar2) }}">
<p>Two variables that contain a value of Bear.</p>
</r:if>
Check if bearVar contains Bear OR if wolfVar contains notARealVar:
We can also combine checks with multiple arguments. This one uses the or operator:
<r:if expression="{{ contains(bearVar, 'Bear') or contains(wolfVar, notARealVar) }}">
<p>Contains a Bear or wolf.</p>
</r:if>
Check if bearVar contains Bear AND wolfVar contains wolf:
And this one uses the and operator:
<r:if expression="{{ contains(bearVar, 'Bear') and contains(wolfVar, 'wolf') }}">
<p>Contains a Bear <strong>and</strong> wolf.</p>
</r:if>
NOTE: expression="" can also be written as expr="" for those who like to abbreviate.
NOTE 2: BLANK, NOT, and CONTAINS are case insensitive. They can be upper or lowercase.
CleanSlate allows us to use expressions inside if and unless tags. Expressions can be used to check:
- if a value is blank
- if a value is not blank
- if a value contains an exact string or variable
- Note:
containsmatches exact strings. If you tried matchingBearandbear, these are not the same strings because one of theb's is capitalized.
- Note:
What can the if statement expression values be?
The expression can evaluate the following:
- Custom Page Data
- Custom Site Data
- Variables
blank & not blank (unless & !=) code
examples:
The following code examples will all use a custom site data value for their expression.
If using custom
page data, be sure to prepend page.data to your variable, eg:
page.data.my_page_data_name.
Check if my_site_data_name is blank:
{% if site.data.my_site_data_name == blank %}
<p>This paragraph will show if <code>my_site_data_name</code> does NOT have content</p>
{% endif %}
Check if my_site_data_name is not blank:
{% unless site.data.my_site_data_name == blank %}
<p>Will show if there is content in site data: {{ site.data.my_site_data_name }}</p>
{% endunless %}
This could also be written inside the expression using if and != instead of unless:
{% if site.data.my_site_data_name != blank %}
<p>Will show if there is content in site data: {{ site.data.my_site_data_name }}</p>
{% endif %}
contains code examples:
The following code examples check variables (which can also be custom page/site data) to see if their value contains the value we are looking for.
To test the following expressions, paste these assign tags above your
expressions:
{% assign bearVar = "Bear" %} <!-- For testing purposes only! -->
{% assign wolfVar = "wolf" %}
{% assign myVar2 = "Bear" %}
Check if bearVar contains “Bear”:
{% if bearVar contains "Bear" %}
<p>Contains a Bear.</p>
{% endif %}
Check if two variables contain the same value:
{% if bearVar contains myVar2 %}
<p>Two variables that contain a value of Bear.</p>
{% endif %}
Check if bearVar contains Bear or if wolfVar contains notARealVar:
We can also combine checks with multiple arguments. This one uses the or operator:
{% if bearVar contains "Bear" or wolfVar contains notArealVar %}
<p>Contains a Bear or wolf.</p>
{% endif %}
Check if bearVar contains Bear and wolfVar contains wolf:
And this one uses the and operator:
{% if bearVar contains "Bear" and wolfVar contains "wolf" %}
<p>Contains a Bear <strong>and</strong> wolf.</p>
{% endif %}
empty code examples:
In addition to blank and contains, Liquid also has empty (specifically for arrays):
{% assign animals = "" | split: "," %}
{% if animals == empty %}
<p><code>animals</code> is empty.</p>
{% endif %}
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.