+813-444-3589+44 118 328 3220+49-231-976765-0 | Contact us

Control flow commands

Sometimes it is convenient to only perform an action if some specific conditions are fulfilled. For that Liquid contains various commands (as a reminder, commands are enclosed with {% … %} tags). All flow commands are block based, which means that all of them need an additional tag at the end of the block (and the name of the command is always 'end' plus the original command name). Usually the flow is controlled depending on a condition, which is a bool expression (the result of the operation is from type bool).

Comparers

Conditions are usually comparisons which are combined using 'and' or 'or'. The result of a comparison is either 'true' or 'false' (depending on whether the comparison is correct (or not)). Comparisons are constructed using the following comparers:

A == B A equals B
A != B A does not equal B
A <> B A does not equal B
A < B A is less than B
A > B A is greater than B
A <= B A is less or equal than B
A >= B A is greater or equal than B
A contains B The list A contains element B or string A contains string B
A startswith B The list A starts with element B or string A starts with string B
A endswith B The list A ends with element B or string A ends with string B
A hasKey B The dictionary A has an element with key/handle B
A hasValue B The dictionary A has an element with the result value B
A The object exists. Testing against the existence of a structure or value, you could also only specify the name of the structure, the result will be true if it exists and false otherwise (of course if A is of type bool, the bool expression is evaluated).

 

Comparison Order

If there are several comparisons (e.g. within an if-clause), then the arguments are evaluated from right to left (please remark that the usage of brackets is not supported). This means if you write 'A and B or C', this could mean '(A and B) or C' or 'A and (B or C)', which are different. In Liquid the order will be from right to left, meaning that the last bool operation (and, or) will be performed first, and then the one left to it, and so on. For an example if you use 'A and B or C and D', the result will be the evaluation of 'A and (B or (C and D))'.

 

 

 

Here comes a list of the various command flow commands with their description and examples.

 

 

if/elsif/else

Use if/elseif/else to create various blocks that will only executed if some condition will result in true.

Example:


{% if product.title == 'apple' %}
  The product is an apple
{% elsif collections['related'] %}
  The collection with handle name 'related' exists and can be accessed safely
{% elsif collection.description == collection.title)
  We can use any number of elsifs and also use different variables to look for
{% else %}
  Everything else
{% endif %}
		

Outputs:


The product is an apple

 

 

unless

Unless is quite similar to the if command, but exactly the other way round. It will render the block if the condition is NOT true. This has no else part, so it is very lightweighted.

Example:


{% unless forloop.last %}
  This will not be written for the last loop iteration
{% endunless %}
		

Output:


This will not be written for the last loop iteration

This would be the same as


{% if forloop.last != true %}
  …
{% endif %}

 

 

case/when/else

Case/when/else are different methods comparable to if/elsif blocks but always checking the same variable against different values. The comparison is initialized with the case command which specifies the variable to compare against to. Each when command will then compare the given variable to the case value.

Example:


{% case product.title %}
{% when 'apple' %}
  It is an apple
{% when 'orange' %}
  It is an orange
{% else %}
  It is neither apple nor orange
{% endcase %}
		

Output:


It is neither apple nor orange

 

More about other commands:

Iteration commands

Variable handling commands

HTML commands