Yoolk Liquid

Home Tags Control-flow-tags

Control Flow Tags

Control Flow tags determine which block of code should be executed based on different conditions.

if

Executes a block of code only if a certain condition is met

Input

{% if product.name == 'Awesome Shoes' %}
  These shoes are awesome!
{% endif %}

Output

These shoes are awesome!

elsif / else

Adds more conditions within an if or unless block.

Input

<!-- If customer.name = 'anonymous' -->
{% if person.name == 'kevin' %}
  Hey Kevin!
{% elsif person.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 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 %}

Output

This is a cake

unless

Similar to if, but executes a block of code only if a certain condition is not met

Input

{% unless product.name == '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.name != 'Awesome Shoes' %}
  These shoes are not awesome.
{% endif %}