Filters

Filters are simple methods that allow you to modify and manipulate the output of a Liquid object or variable. They can be applied to an object or variable using the pipe character | followed by the filter name and any optional parameters. Filters can be chained together to perform multiple operations on the data.

Basic Syntax

The basic syntax for using a filter is:

{{ object | filter_name: parameter1, parameter2, ... }}

Example: Using the replace filter to replace "example" with "sample" in a string

{{ "This is an example text." | replace: "example", "sample" }}

Output:

This is a sample text.

Chaining Filters

You can chain multiple filters together by adding additional pipe characters | and filter names with their parameters. The filters will be applied in the order they are written.

Example: Using the upcase filter and the replace filter together

{{ "This is another example." | upcase | replace: "EXAMPLE", "sample" }}

Output:

THIS IS ANOTHER sample.

Built-in Filters

Liquid comes with a variety of built-in filters for common tasks such as string manipulation, formatting numbers, and working with dates. Some examples of built-in filters include capitalize, truncate, round, and date.

Example: Using the truncate filter to limit a string to 10 characters

{{ "This is a very long text." | truncate: 10 }}

Output:

This is a...

For more details on filters, including a comprehensive list of built-in filters, please refer to the LiquidJS documentation:

LiquidJS Filters Documentation

By using filters in your Liquid templates, you can easily manipulate data and create dynamic content based on various conditions, greatly enhancing the functionality of your web application.

Last updated