Home > Shopify Development > Liquid case when statement

How to use liquid case when statement in Shopify

Sam Nguyen
Sam Updated: July 08, 2024

Share:

Drive 20-40% of your revenue with Avada
avada email marketing

Liquid has become a backbone of Shopify since 2006 with a lot of good reviews from website application. Liquid provides you with three main sources to upload the dynamic content on your front page, hence today tutorial will dig into one of the most noticeable features which is control flow with case/when statement.

Table of content

General information about Liquid codes and tag types

Liquid codes are divided into objects, tags and filters. Double curly braces denote objects and variable names. Filters change a Liquid object’s output. There are five main types of filters including string, number, boolean, nil and array. Tags create logic and control flow for a template. Tags begin with two curly braces and percent signs. Tags is classified into control flow, iteration and variable assignment.

This tutorial focuses on control flow function by guiding you to use case/when statements.

How to use case when statement

Case/when statements create a switch statement to compare a variable with different values. In fact, case starts with switching statement and when compares its values. Your web will show the text or letter that you write in the first line of your code, if the input sentence is not valid, the web will not show.

Input


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

Output


This is a cake

Conclusion

To sum up, the tutorial aims to guide you to use case/when statements. Case/when statements allows you to display right content if you insert characters which are the same as the initial ones. We hope that our article is helpful to you. Let’s us know your difficulties and we will address them.


Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.

Stay in the know

Get special offers on the latest news from AVADA.