Control flow


case

Creates a switch statement to execute a particular block of code when a variable has a specified value. case initializes the switch statement, and when statements define the various conditions.

An optional else statement at the end of the case provides code to execute if none of the conditions are met.

Input

 {% assign handle = "cake" %}
 {% case handle %}
   {% when "cake" %}
     This is a cake
   {% when "cookie", "biscuit" %}
     This is a cookie
   {% else %}
     This is not a cake nor a cookie
 {% endcase %}

Output

 This is a cake

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!

You can add more conditions within if block with elsif or else

Input

 {% if false %}
   This won't render...
 {% elsif true %}
   ...but this will
 {% endif %}

Output

 ...but this will

unless

Input

 {% unless product.title == "Awesome Shoes" %}
   These shoes are not awesome!
 {% endif %}

Output

 These shoes are not awesome!

You can add more conditions within unless block with elsif or else

Input

 {% unless false %}
   This will render...
 {% elsif true %}
   ...but this won't
 {% endif %}

Output

 This will render...