Video outline

We can change how data is outputted/printed by adding a filter to it. This removes the need to create a new variable in some cases as we can just use a filter when printing.

Filter can be used on numbers, strings, arrays, collections and dates. Each of these data types have different specific filters.
Filters are generally used in a print statement (between curly brackets).

In theory, they can also be used within an assign statement to already modify a variable to the correct output, but we advise against this.

It’s best to keep the data in the assigned variable as pure as possible and just modify to the desired output by using a filter in the print statement

Filter syntax

Filter syntax is as follows. The pipe forms the delimiter between the main variable and the code which we refer to as the filter.

{{ "My string" | append:"is the best" }}

Types of filters

A filter is just a generic name of all types of code we can put to the right side of the pipe in cases just like we saw on the previous slide. Many types of filters exist.

One important type is the ‘currency’ filter, which is shown here. A currency filter has a couple of important properties in liquid you will learn about later.

A currency filter is to be used on numbers only and will not work on variables that contain strings.

{{ 10563 | currency }}

There are also other types of number filters. One example is the ‘abs’ filter which returns the absolute value of the variable.

{{ -100 | abs }}

An often used filter specifically for strings is ‘append’. ‘Append’ will extend the value of your string variable with the string specified in your filter, just as shown here.

{{ "My string" | append:"is the best" }}

Another useful string filter is the ‘remove’ filter. This filter lets you remove a specific combination of symbols from the string you are applying your filter to. The remove filter also works for just one symbol.

{{ "BE.0696.123.456" | remove:"." }}

There are many more types of filters, elaborating on all of these extends beyond the scope of this course.

Conclusion

  • Filters are useful when the output of an existing variable needs to be modified, without creating a new variable.
  • Most often we use filters in print statements.
  • There are many types of filters. Check them all out here .