- 31 Mar 2025
- 3 minute read
- Print
- DarkLight
- PDF
The If, Elsif, and Else Tags
- Updated 31 Mar 2025
- 3 minute read
- Print
- DarkLight
- PDF
The if
, elsif
, and else
tags are fundamental to Liquid markup and are essential components for conditional logic.
if
The if
tag creates simple conditional logic, and is composed of two basic pieces: the condition and the output.
The condition and the output are arranged like so:
{% if condition %}
output
{% endif %}
The condition sets the logical requirements for the output’s appearance. If the condition evaluates to true, the output appears, otherwise, it does not.
Consider the following example:
{% if {{app_program}} == 'Art' %}
Welcome to Slate University's Fine Arts Program!
{% endif %}
The condition, if {{app_program}} == 'Art'
, is saying:
If the value for the
app_program
export isArt
, then outputWelcome to Slate University’s Fine Arts Program!
.If the value in the condition statement is not
Art
, then output nothing.
{% endif %}
Every opened tag must have its closing tag: for if
, that closing is {% endif %}
.
📝 Without the closing tag, the conditional statement won’t function as intended.
elsif
The elsif
tag is another basic component in Liquid markup that goes one step further than if
: it allows for the evaluation of multiple conditions.
elsif
functions much like the if
tag; however, unlike the if
tag, it can not be used as a standalone tag. It must always be preceded by an if
.
An elsif
tag, just like an if
tag, is composed of a condition and output, but in this case, there may be multiple conditions and multiple outputs.
Consider the following example:
{% if {{app_program}} == 'Art' %}
Welcome to Slate University's Fine Arts Program!
{% elsif {{app_program}} == 'Medicine' %}
Welcome to Slate University's School of Medicine!
{% endif %}
This set of conditions can be translated as:
If the value for the
app_program
export isArt
, then outputWelcome to Slate University’s Fine Arts Program!
.If that’s not the case, but the value for the
app_program
export is insteadMedicine
, then outputWelcome to Slate University’s School of Medicine!
.If neither condition is true, then output nothing.
Multiple elsif
tags
You can use as many elsif
tags as you need.
At some point though, especially in the case of programs or majors, it can be more beneficial to use merge fields and Configurable Joins to dynamically display the value, rather than using Liquid markup to account for every possibility.
else
The else
tag is also not a standalone component and must be accompanied by an if
statement.
else
is a final or catchall tag. The else
tag does not have a condition, like the if
tag and elsif
tags, and must be placed at the end of the full statement.
There are two ways of using the else
tag in Liquid markup.
The first:
{% if {{app_program}} == 'Art' %}
Welcome to Slate University's Fine Arts Program!
{% else %}
Welcome to Slate University!
{% endif %}
This can be translated as:
If the value for the
app_program
export isArt
, then outputWelcome to Slate University’s Fine Arts Program!
.If it doesn’t have that value, just output
Welcome to Slate University!
.
The second:
{% if {{app_program}} == 'Art' %}
Welcome to Slate University's Fine Arts Program!
{% elsif {{app_program}} == 'Medicine' %}
Welcome to Slate University's School of Medicine!
{% elsif {{app_program}} == 'Biology' %}
Welcome to Slate University's School of Science!
{% else %}
Welcome to Slate University!
{% endif %}
This second example can be translated as:
If the value for the
app_program
export isArt
, then outputWelcome to Slate University's Fine Arts Program!
.If that’s not the case, but the value for the
app_program
export is insteadMedicine
, then outputWelcome to Slate University's School of Medicine!
.If that’s not the case, but the value for the
app_program
export is insteadBiology
, then outputWelcome to Slate University's School of Science!
.If none of these conditions is true, output
Welcome to Slate University!
.
The second of these examples can become cumbersome if there are a large number of different values to evaluate. In some cases, it may be more beneficial and efficient to use merge fields to display information dynamically.