2018-03-04 22:43:39 +08:00
< h2 > Comments< / h2 >
< pre > < code > {% comment %}This is a comment{% endcomment %}< / code > < / pre >
< h2 > Control Flow< / h2 >
2020-03-07 18:53:31 +08:00
< p > Liquid provides multiple control flow statements.< / p >
2018-03-04 22:43:39 +08:00
< h3 > if< / h3 >
< pre > < code >
{% if customer.name == 'kevin' %}
Hey Kevin!
{% elsif customer.name == 'anonymous' %}
Hey Anonymous!
{% else %}
Hi Stranger!
{% endif %}
< / code > < / pre >
< h3 > unless< / h3 >
2020-03-07 18:53:31 +08:00
< p > The opposite of < code > if< / code > – executes a block of code only if a certain condition is not met.< / p >
2018-03-04 22:43:39 +08:00
< pre > < code >
{% unless product.title == 'Awesome Shoes' %}
These shoes are not awesome.
{% endunless %}
< / code > < / pre >
< h3 > case< / h3 >
2020-03-07 18:53:31 +08:00
< p > Creates a switch statement to compare a variable with different values. < code > case< / code > initializes the switch statement, and < code > when< / code > compares its values.< / p >
2018-03-04 22:43:39 +08:00
< pre > < code >
{% 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 %}
< / code > < / pre >
< h3 > for< / h3 >
2020-03-07 18:53:31 +08:00
< p > Repeatedly executes a block of code.< / p >
2018-03-04 22:43:39 +08:00
2020-03-07 18:53:31 +08:00
< p > break = Causes the loop to stop iterating when it encounters the break tag. < br >
continue = Causes the loop to skip the current iteration when it encounters the continue tag.< / p >
2018-03-04 22:43:39 +08:00
< pre > < code >
{% for i in (1..10) %}
{% if i == 4 %}
{% break %}
{% elsif i == 6 %}
{% continue %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
< / code > < / pre >
< h3 > range< / h3 >
< pre > < code >
{% for i in (3..5) %}
{{ i }}
{% endfor %}
{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}
< / code > < / pre >