How to use liquid if else/ elsif statements in Shopify

Liquid has been used in Shopify since 2006 and becomes a popular language template for many 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 are control flow with if/else statement.

Table of content

General information about Liquid codes and tag types

Liquid codes are classified into objects, tags, and filters. Objects tell Liquid where the content is shown on a page. Double curly braces denote objects and variable names. Filters change a Liquid object’s output. Tags create logic and control flow for a template. Tags begin with two curly braces and percent signs. Tags can be classified into three types: control flow, iteration, and variable assignment.

How to use if else/elsif statement

In control flow, you can use statement of if else, elsif, unless and case to site and show conflicting syntax on your front page.

This tutorial will guide you to use if else/elsif statement. First, If executes a block of codes in case a certain condition is true. For example, you want to add awesome shoes as a title of the product so that buyers can feel the feeling of wearing this pair of shoes.

Input



{% if product.title == 'Awesome Shoes' %}
  These shoes are awesome!
{% endif %}


Output


These shoes are awesome!

Second, Elsif or else adds more condition within an if or unless block. For instance, a client who is not a member of your closed group signs in and you want him/her to know that he/she is a stranger to the thí group, then you use elsif/else to inform.

Input


<!-- If customer.name = 'anonymous' -->
{% if customer.name == 'kevin' %}
  Hey Kevin!
{% elsif customer.name == 'anonymous' %}
  Hey Anonymous!
{% else %}
  Hi Stranger!
{% endif %}


Output


Hey Anonymous!

Conclusion

In conclusion, if/ else statement responds to the logic of your website. You can use this statement to filter those being strange to your secrete group. We hope that you will find ways to build your page and let us know about your difficulties, and we will try to address them.