Liquid is a template language written in Ruby. It is available as an open-source project and is used by many software projects. In VersaCommerce, Liquid is the basis of all themes: it loads dynamic content into the pages of an online shop.
What is a template language?
A template language lets you build web pages that combine static content, which is the same on many pages, with dynamic content, which changes from page to page. The static elements that define the layout are written in HTML, the dynamic elements in Liquid. The Liquid elements act as placeholders: when the file is rendered and sent to the browser, Liquid is replaced with data from the VersaCommerce shop the theme is installed in.
Use cases
- Working with VersaCommerce themes
- Building a complete VersaCommerce theme
- Changing an existing theme. Merchants often ask developers to adapt a theme to their needs.
Liquid syntax
Like other programming languages, Liquid has a syntax, works with variables and has constructs for output and logic. Liquid code is easy to tell apart from HTML by its delimiters:
- Double curly braces
{{ }}mark output. - Curly braces with percent signs
{% %}mark logic and control flow.
Liquid code consists of three main parts:
Objects
Liquid objects output data from the shop. In a template, objects are wrapped in double curly braces {{ }}:
{{ product.title }}
In this example, product is the object and title is one of its properties. Every object has a list of properties. See the product object for all properties of product.
The object {{ product.title }} is used in the product template of a theme. On a product page, the output is the title of the product, for example:
Great T-Shirt
The same template is used for every product, but the objects output different data depending on the product page that is shown.
See Liquid objects for all objects you can use in theme templates.
Tags
Liquid tags create logic and control flow in templates. The percent delimiters {% %} and the text between them produce no visible output. This lets you assign variables and create conditions or loops without showing the Liquid logic on the page.
For example, you can use tags to show different content depending on whether a product is available:
{% if product.available %}
<h2>Price: €99.99</h2>
{% else %}
<h2 class="sold-out">Sorry, this product is sold out.</h2>
{% endif %}
If the product is available, the output is:
Price: €99.99
If the product is not available, the output is:
Sorry, this product is sold out.
The example uses the control flow tags if and else.
Liquid tags fall into these groups:
Filters
Liquid filters change the output of numbers, strings, objects and variables. They are used inside an output tag {{ }} and start with a pipe character |.
A simple example is the string filter capitalize:
{{ 'hello, world!' | capitalize }}
The filter capitalizes the first letter of the string. The output is:
Hello, world!
You can chain several filters. They are applied from left to right:
{{ 'hello, world!' | capitalize | remove: 'world' }}
The string is capitalized first, then the word world is removed. The output is:
Hello, !
Liquid filters fall into these groups: