Control Flow
  • 13 Mar 2024
  • 1 Minute to read
  • Dark
    Light

Control Flow

  • Dark
    Light

Article summary

if

Executes a block of code only if a certain condition is true.

Input

{% if product.title == "Awesome Shoes" %}

These shoes are awesome!

{% endif %}

Output

These shoes are awesome! 

unless

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

Input

{% unless product.title == "Awesome Shoes" %}

These shoes are not awesome.

{% endunless %}

Output

These shoes are not awesome.

This would be the equivalent of doing the following:

{% if product.title != "Awesome Shoes" %}

These shoes are not awesome.

{% endif %}

elsif / else

Adds more conditions within an if or unless block.

Input

<!-- If customer.name = "anonymous" -->

{% if customer.name == "kevin" %}

Hey Kevin!

{% elsif customer.name == "anonymous" %}

Hey Anonymous!

{% else %}

Hi Stranger!

{% endif %}

Output

Hey Anonymous!

case/when

Creates a switch statement to compare a variable with different values. case initialises 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 %}

Output

This is a cake


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence