Comments
{% comment %}This is a comment{% endcomment %}
Control Flow
Liquid provides multiple control flow statements.
if
{% if customer.name == 'kevin' %}
Hey Kevin!
{% elsif customer.name == 'anonymous' %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
unless
The opposite of if
– executes a block of code only if a certain condition is not met.
{% unless product.title == 'Awesome Shoes' %}
These shoes are not awesome.
{% endunless %}
case
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 %}
for
Repeatedly executes a block of code.
break = Causes the loop to stop iterating when it encounters the break tag.
continue = Causes the loop to skip the current iteration when it encounters the continue tag.
{% for i in (1..10) %}
{% if i == 4 %}
{% break %}
{% elsif i == 6 %}
{% continue %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
range
{% for i in (3..5) %}
{{ i }}
{% endfor %}
{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}