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).


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 == BA equals B
A != BA does not equal B
A <> BA does not equal B
A < BA is less than B
A > BA is greater than B
A <= BA is less or equal than B
A >= BA is greater or equal than B
A contains BThe list A contains element B or string A contains string B
A startswith BThe list A starts with element B or string A starts with string B
A endswith BThe list A ends with element B or string A ends with string B
A hasKey BThe dictionary A has an element with key/handle B
A hasValue BThe dictionary A has an element with the result value B
AThe 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.



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


{% 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 %}


The product is an apple


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 light weighted.


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


This will not be written for the last loop iteration

This would be the same as

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



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.


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


It is neither apple nor orange


More about other commands: