This guide introduces you to the basics of Shopify Liquid, demonstrates how to use it to customize themes, and provides practical examples of common use cases like dynamically displaying product information.
Shopify Liquid is the cornerstone of Shopify theme development, providing a powerful, flexible, and easy-to-learn templating language for customizing Shopify stores. Whether you’re a store owner looking to tweak your theme or a developer creating custom themes, understanding Liquid will unlock endless possibilities for personalizing your storefront.
This guide introduces you to the basics of Shopify Liquid, demonstrates how to use it to customize themes, and provides practical examples of common use cases like dynamically displaying product information.
Liquid is an open-source template language created by Shopify. It acts as a bridge between the data in your Shopify store and the HTML displayed on your storefront. With Liquid, you can:
Liquid is not a programming language but rather a templating language that mixes HTML with tags, objects, and filters to output dynamic content.
Liquid tags are used to control logic and flow. They do not generate visible output but are essential for loops, conditions, and variables.
Example of a Tag:
{% if product.available %}
In Stock
{% else %}
Out of Stock
{% endif %}
Objects are the dynamic pieces of content retrieved from your Shopify store. They are outputted using double curly braces {{ }}
.
Example of an Object: liquid Copy code
<p>{{ product.title }}</p>
This outputs the title of the current product.
Filters modify the output of an object, such as formatting text or numbers.
Example of a Filter:
<p>Price: {{ product.price | money }}</p>
This formats the product price as currency.
To start using Liquid, access your theme files:
product.liquid
or collection.liquid
.A common use case is dynamically showing product details on a product page.
Example: Display Product Title and Description
<h1>{{ product.title }}</h1>
<p>{{ product.description }}</p>
Example: Display Price and Availability liquid Copy code
<p>Price: {{ product.price | money }}</p>
{% if product.available %}
<p>In Stock</p>
{% else %}
<p>Out of Stock</p>
{% endif %}
Use Liquid to loop through products in a collection and display them in a grid.
Example Code:
<div class=”product-grid”>
{% for product in collection.products %}
<div class=”product-item”>
<h2>{{ product.title }}</h2>
<p>{{ product.price | money }}</p>
<a href=”{{ product.url }}”>View Product</a>
</div>
{% endfor %}
</div>
This code loops through all products in a collection and displays their title, price, and link.
You can use Liquid to dynamically show a welcome message for logged-in customers.
Example Code:
{% if customer %}
<p>Welcome back, {{ customer.first_name }}!</p>
{% else %}
<p>Welcome to our store! <a href=”/account/login”>Login</a></p>
{% endif %}
Liquid allows you to show or hide elements based on certain conditions, such as whether a product is on sale.
Example: Show Sale Badge liquid Copy code
{% if product.compare_at_price > product.price %}
<span class=”badge”>On Sale</span>
{% endif %}
Enhance the shopping experience by suggesting related products.
Example: liquid Copy code
<h3>You may also like:</h3>
<ul>
{% for related_product in product.collections[0].products %}
{% unless related_product.id == product.id %}
<li>
<a href=”{{ related_product.url }}”>{{ related_product.title }}</a>
</li>
{% endunless %}
{% endfor %}
</ul>
Loops allow you to iterate over a set of items, such as all products in a collection.
Example: Loop Through Collections liquid Copy code
<ul>
{% for collection in collections %}
<li><a href=”{{ collection.url }}”>{{ collection.title }}</a></li>
{% endfor %}
</ul>
Filters can manipulate data output, such as formatting text or numbers.
Truncate a string: liquid Copy code
{{ product.description | truncate: 100 }}
Capitalize text: liquid Copy code
{{ product.title | capitalize }}
The include
tag allows you to break your theme into smaller reusable snippets.
Example: Include a Banner Snippet Create a snippet called banner.liquid: liquid Copy code
<div class=”banner”>{{ banner_text }}</div>
Include it in a template: liquid Copy code
{% include ‘banner’ with ‘Welcome to our store!’ %}
{% comment %} This section displays product details {% endcomment %}
3. Debug with Output Tags: Print variables to check their values.
{{ product | json }}
Shopify Liquid is an essential tool for creating a unique, professional Shopify store. By learning its basics and applying practical use cases like dynamic product displays, conditional content, and related product suggestions, you can significantly enhance your store’s functionality and user experience.
Whether you’re a beginner or an experienced developer, mastering Shopify Liquid will open doors to limitless customization. Start experimenting with the examples in this guide and gradually explore more advanced features to take your store to the next level.