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.
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
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.