What are filters?


Filters provide a way for you to apply a function over a variable before it's rendered to the page. In order to use a filter you will have to call it following a variable declaration and separated by a |. Most common use-case for this is the need to modify or format the variable content before it's included in the template. For example, let's say that you wanted to display a ISO date in a more human readable format:

Standard date: {{ order.place }}
Formatted date: {{ order.place | date }}

In the example above, we are using the date filter and it will render to the customer as:

Standard date: 2020-05-10
Formatted date: May 10, 2020

Filter with parameters

Since filters are just a way for you to apply a function over a variable, some filters also support the idea of additional parameters. You can supply additional parameters by passing them into the filter, the same way as you would in other programming paradigms. A good example of this is the date filter from our earlier example. It accepts a date format as a parameter that allows you to have greater flexibility over how the date is displayed. Please find a few examples below:

Localized format 1: {{ order.place | date('L') }}
Localized format 2: {{ order.place | date('LL') }}
Non-localized specific format: {{ order.place | date('MMM D YYYY') }}

In the example above, we're displaying a date in two different localized formats and a non-localized one. You can see the resulting message below if the current locale was set to English.

Localized format: 01/10/2020
Localized format: January 10, 2020
Non-localized specific format: Jan 10 2020

You can read more about our list of supported filters in the Filter Reference


What’s Next