Control flow tags create conditions that decide whether blocks of Liquid code get executed.
Control flow tags:
- if
- unless
- else / elsif
- case / when
if
Executes a block of code only if a certain condition is true
.
{% if Campaign.Name == "Awesome Campaign" %}
This campaign is awesome!{% endif %}
This campaign is awesome!
unless
The opposite of if
: executes a block of code only if a certain condition is not met.
{% unless Campaign.Name == "Awesome Campaign" %}
This campaign is not awesome.{% endunless %}
This campaign is not awesome.
This would be the equivalent of doing the following:
{% if Campaign.Name != "Awesome Campaign" %}
This campaign is not awesome.{% endif %}
else/elsif
Adds more conditions within an if
or unless
block.
{% if User.Name == "John" %}
Hey John!{% elsif User.Name == "Stacy" %}
Hey Stacy!{% else %}
Hey stranger!{% endif %}
If the user's name is Stacy, the output will be:
Hey Stacy!
case/when
Creates a switch statement to compare a variable with different values. case
initializes the switch statement, and when
compares its values.
{% assign handle = "cake" %}
{% case handle %}
{% when "cake" %}
This is a cake {% when "cookie" %}
This is a cookie {% else %}
This is not a cake nor a cookie{% endcase %}
If the user's name is Stacy, the output will be:
This is a cake