{% case %}

Use the {% case %} tag to execute a particular block of code when a variable has a specified value.

Inside the {% case %} tag, use the {% when %} tag to define the values to check the variable against and the code blocks to execute if it matches.

Optionally, at the end of the {% case %} tag, use an {% else %} tag to execute a block of code if none of the conditions inside the {% case %} tag were met.

To close the {% case %} tag, use {% endcase %}.

Syntax

{% case [variable] %}{% when option1 [or ...] %}...{% when option2 [, ...] %}...{% else %}...{% endcase %}

You can use the or operator to include more than one condition in a single when block. Each condition can only perform a single == comparison, so if you want to create more complex conditions (eg: contains, >=, etc...) you will need to use an if tag instead.

The case tag creates a new child scope.

Examples

Case statement comparing custom field to string

Copy
{% case page.title_type.value %} {% when 'fullwidth' %} This is full width title {% when 'halfwidth' or 'quarterwidth' %} This is partial width title {% when "empty", "none", "null", "skip" %} {% comment %}No title{% endcomment %} {% else %} This title does not have a span {% endcase %}