Liquid filters
Filters are simple methods that modify the output of numbers, strings, variables and objects. They are placed within an output tag {{
}}
and are denoted by a pipe character |
.
In the example above, item
is the object, title
is its attribute, and upcase
is the filter being applied.
Some filters require a parameter to be passed.
Multiple filters can be used on one output. They are applied from left to right.
Array filters
Array filters change the output of arrays. Array is a synonym to list. For example, order.line_items
is an array of line items or order.transactions
is an array of transactions.
join
Joins the elements of an array with the character passed as the parameter. The result is a single string.
first
Returns the first element of an array.
You can use first
with dot notation when you need to use the filter inside a tag.
last
Returns the last element of an array.
You can use last
with dot notation when you need to use the filter inside a tag.
Using last
on a string returns the last character in the string.
concat
Concatenates (combines) an array with another array. The resulting array contains all the elements of the original arrays. concat
will not remove duplicate entries from the concatenated array unless you also use the uniq
filter.
You can string together multiple concat
filters to combine more than two arrays:
index
Returns the item at the specified index location in an array. Note that array numbering starts from zero, so the first item in an array is referenced with [0]
.
map
Accepts an array element's attribute as a parameter and creates an array out of each array element's value.
flat_map ⭐
Creates a flattened array of values taken from the specified attribute of each element of the input array.
group_by ⭐
Groups elements of the same property value. Creates an object where keys are the specified property value, and values are arrays of elements with the same property value.
map_values ⭐
Transforms an object by running each of the object's property value by a given filter. First parameter of map_values
is the filter name. The filter receives the object values one by one as its first parameter. All remaining parameters provided to map_values
are passed down to the given filter.
reverse
Reverses the order of the items in an array.
size
Returns the size of a string (the number of characters) or an array (the number of elements).
You can use size
with dot notation when you need to use the filter inside a tag.
sort
Sorts the elements of an array by a given attribute of an element in the array.
The order of the sorted array is case-sensitive.
where
Creates an array including only the objects with a given property value, or any truthy value by default.
You can use a property name with where
that has no target value when that property is a boolean or truthy. For example, the available
property of products.
Example
uniq
Removes any duplicate instances of elements in an array.
Format filters
date
Converts a timestamp into a specified date format.
e164 ⭐
Formats a phone number according to the E.164 standard. If a country calling code is not included in the phone number, then you can pass an ISO 3166 country code as an additional parameter.
To format a phone number associated with a shipping address:
iso3166_alpha3 ⭐
Converts a two-letter ISO 3166 country code to its three-letter equivalent.
To format a shipping address country as an ISO 3166 alpha-3 country code:
json
Converts a string, or object, into JSON format.
By default it produces a single-line JSON. You can pass an optional parameter "pretty"
, to get a multi-line, indented JSON output.
The json
filter is useful for debugging to check what are all available properties of a given object.
moment ⭐
Returns a current or specified time converted to a desired time zone.
The filter has two parameters: a date format and a time zone identifier.
Math filters
Math filters allow you to apply mathematical tasks.
Math filters can be linked and, as with any other filters, are applied in order of left to right. In the example below, minus
is applied first, then times
, and finally divided_by
.Copy
abs
Returns the absolute value of a number.
abs
will also work on a string if the string only contains a number.
at_most
Limits a number to a maximum value.
at_least
Limits a number to a minimum value.
ceil
Rounds an output up to the nearest integer.
Liquid tries to convert the input to a number before the filter is applied.
divided_by
Divides an output by a number. The output is rounded down to the nearest integer.
floor
Rounds an output down to the nearest integer.
minus
Subtracts a number from an output.
modulo
Divides an output by a number and returns the remainder.
plus
Adds a number to an output.
round
Rounds the output to the nearest integer or specified number of decimals.
times
Multiplies an output by a number.
to_fixed
Convert a number into a string, rounding the number to keep only the given number of decimals.
The difference between round
and to_fixed
it that round
returns a number so it trims trailing zeros while to_fixed
will preserve them.
String filters
String filters are used to manipulate outputs and variables of the string type.
append
Appends characters to a string.
capitalize
Capitalizes the first word in a string
downcase
Converts a string into lowercase.
escape
Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape.
extract_number ⭐
Extracts a first number from a string. It works for integers (123), decimal numbers (123.45), and negative numbers (-123). The extracted number is still a text so it doesn't strip leading zeros (0123).
extract_numbers ⭐
Extracts all numbers from a string. It works for integers (123), decimal numbers (123.45), and negative numbers (-123). The extracted numbers are still a text to preserve leading zeros (0123). The result is an array.
md5
Calculates an MD5 hash from a string.
An example use case for this filter is to calculate a checksum of a request payload, and include it in a request header. Such a checksum is required by some APIs, for example, the Richard Photo Lab API.
newline_to_br
Inserts a <br > linebreak HTML tag in front of each line break in a string.
prepend
Adds the specified string to the beginning of another string.
remove
Removes all occurrences of a substring from a string.
remove_first
Removes only the first occurrence of a substring from a string.
replace
Replaces all occurrences of a string with a substring.
replace_first
Replaces the first occurrence of a string with a substring.
slice
The slice
filter returns a substring, starting at the specified index. An optional second parameter can be passed to specify the length of the substring. If no second parameter is given, a substring of one character will be returned.
If the passed index is negative, it is counted from the end of the string.
split
The split
filter takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array filters.
strip
Strips tabs, spaces, and newlines (all whitespace) from the left and right side of a string.
lstrip
Strips tabs, spaces, and newlines (all whitespace) from the left side of a string.
rstrip
Strips tabs, spaces, and newlines (all whitespace) from the right side of a string.
strip_all ⭐
Strips tabs, spaces, and newlines (all whitespace) from the entire string.
strip_html
Strips all HTML tags from a string.
strip_newlines
Removes any line breaks/newlines from a string.
truncate
Truncates a string down to the number of characters passed as the first parameter. An ellipsis (...) is appended to the truncated string and is included in the character count.
Custom ellipsis
truncate
takes an optional second parameter that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (...), but you can specify a different sequence.
The length of the second parameter counts against the number of characters specified by the first parameter. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first parameter of truncate
, since the ellipsis counts as 3 characters.
No ellipsis
You can truncate to the exact number of characters specified by the first parameter and show no trailing characters by passing a blank string as the second parameter:
truncatewords
Truncates a string down to the number of words passed as the first parameter. An ellipsis (...) is appended to the truncated string.
Custom ellipsis
truncatewords
takes an optional second parameter that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (...), but you can specify a different sequence.
No ellipsis
You can avoid showing trailing characters by passing a blank string as the second parameter:
upcase
Converts a string into uppercase.
url_decode
Decodes a string that has been encoded as a URL or by url_encode.
url_encode
Converts any URL-unsafe characters in a string into percent-encoded characters.
Note that url_encode
will turn a space into a +
sign instead of a percent-encoded character.
xml_escape
This is a special filter available in Exporteo to facilitate the setup of an XML output template.
Replaces characters that are special characters in XML documents.
Last updated