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:
Example: Using the replace
filter to replace "example" with "sample" in a string
Output:
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
Output:
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
Output:
For more details on filters, including a comprehensive list of built-in filters, please refer to the LiquidJS 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