How can we help?

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.

Input

{% if Campaign.Name == "Awesome Campaign" %}
    This campaign is awesome!
{% endif %}

Output

This campaign is awesome!

unless

The opposite of if: executes a block of code only if a certain condition is not met.

Input

{% unless Campaign.Name == "Awesome Campaign" %}
    This campaign is not awesome.
{% endunless %}

Output

This campaign is not awesome.

This would be the equivalent of doing the following:

Input

{% if Campaign.Name != "Awesome Campaign" %}
    This campaign is not awesome.
{% endif %}

else/elsif

Adds more conditions within an if or unless block.

Input

{% 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:

Output

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.

Input

{% 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:

Output

This is a cake