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 |
.
<!-- item.title = "Invisible Watch" -->
{{ item.title | upcase }}
INVISIBLE WATCH
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.
{{ item.title | remove: "Invisible" }}
Watch
Multiple filters can be used on one output. They are applied from left to right.
<!-- item.title = "Invisible Watch" -->
{{ item.title | upcase | remove: "INVISIBLE" }}
WATCH
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.
{{ product.tags | join: ', ' }}
tag1, tag2, tag3
first
Returns the first element of an array.
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | first }}
sale
You can use first
with dot notation when you need to use the filter inside a tag.
{% if product.tags.first == "sale" %}
This product is on sale!
{% endif %}
last
Returns the last element of an array.
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | last }}
awesome
You can use last
with dot notation when you need to use the filter inside a tag.
{% if product.tags.last == "sale"%}
This product is on sale!
{% endif %}
Using last
on a string returns the last character in the string.
<!-- product.title = "Awesome Shoes" -->
{{ product.title | last }}
s
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.
{% assign fruits = "apples, oranges, peaches, tomatoes" | split: ", " %}
{% assign vegetables = "broccoli, carrots, lettuce, tomatoes" | split: ", " %}
{% assign plants = fruits | concat: vegetables %}
{{ plants | join: ", " }}
apples, oranges, peaches, tomatoes, broccoli, carrots, lettuce, tomatoes
You can string together multiple concat
filters to combine more than two arrays:
{% assign fruits = "apples, oranges, peaches" | split: ", " %}
{% assign vegetables = "broccoli, carrots, lettuce" | split: ", " %}
{% assign animals = "dogs, cats, birds" | split: ", " %}
{% assign things = fruits | concat: vegetables | concat: animals %}
{{ things | join: ", " }}
apples, oranges, peaches, broccoli, carrots, lettuce, dogs, cats, birds
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]
.
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags[2] }}
womens
map
Accepts an array element's attribute as a parameter and creates an array out of each array element's value.
<!-- collection.title = "Spring", "Summer", "Fall", "Winter" -->
{% assign collection_titles = collections | map: 'title' %}
{{ collection_titles }}
SpringSummerFallWinter
flat_map ⭐
Creates a flattened array of values taken from the specified attribute of each element of the input array.
<!--
order = {
line_items: [{
discount_allocations: [{
amount: 1.23
}, {
amount: 2.34
}]
}, {
discount_allocations: [{
amount: 3.45
}]
}]
}
-->
{{ order.line_items | flat_map: "discount_allocations" | json }}
[{"amount":1.23},{"amount":2.34},{"amount":3.45}]
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.
<!--
order = {
line_items: [{
price: 1.23,
vendor: "A"
}, {
price: 2.34,
vendor: "B"
}, {
price: 3.45,
vendor: "A"
}]
}
-->
{{ order.line_items | group_by: "vendor" | json }
{"A":[{"price":1.23,"vendor":"A"},{"price":3.45,"vendor":"A"}],"B":[{"price":2.34,"vendor":"B"}]}
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.
<!--
dimensions = { depth: 3, height: 5, width: 7 }
-->
{% assign doubled = dimensions | map_values: "times", 2 %}
{{ doubled.depth }}
{{ doubled.height }}
{{ doubled.width }}
6
10
14
reverse
Reverses the order of the items in an array.
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | reverse | join: ", " }}
plums, peaches, oranges, apples
size
Returns the size of a string (the number of characters) or an array (the number of elements).
{{ 'The quick brown fox jumps over a lazy dog.' | size }}
42
You can use size
with dot notation when you need to use the filter inside a tag.
{% if collections.frontpage.products.size > 10 %}
There are more than 10 products in this collection!
{% endif %}
sort
Sorts the elements of an array by a given attribute of an element in the array.
{% assign products = collection.products | sort: 'price' %}
{% for product in products %}
<h4>{{ product.title }}</h4>
{% endfor %}
The order of the sorted array is case-sensitive.
<!-- products = "a", "b", "A", "B" -->
{% assign products = collection.products | sort: 'title' %}
{% for product in products %}
{{ product.title }}
{% endfor %}
A B a b
where
Creates an array including only the objects with a given property value, or any truthy value by default.
All products:
{% for product in collection.products %}
- {{ product.title }}
{% endfor %}
{% assign kitchen_products = collection.products | where: "type", "kitchen" %}
Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %}
All products:
- Vacuum
- Spatula
- Television
- Garlic press
Kitchen products:
- Spatula
- Garlic press
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
{% assign available_products = collection.products | where: "available" %}
Available products:
{% for product in available_products %}
- {{ product.title }}
{% endfor %}
uniq
Removes any duplicate instances of elements in an array.
{% assign fruits = "orange apple banana apple orange" %}
{{ fruits | split: ' ' | uniq | join: ' ' }}
orange apple banana
Format filters
date
Converts a timestamp into a specified date format.
format
description
example value
%d
Two-digit day of the month (with leading zeros)
01
to 31
%m
Two digit representation of the month
01
to 12
%y
Two digit representation of the year
21
%Y
Four digit representation for the year
2021
%H
Two digit representation of the hour in 24-hour format
00
to 23
%I
Two digit representation of the hour in 12-hour format
01
to 12
%p
Upper-case 'AM' or 'PM' based on the given time
AM
or PM
%P
Lower-case 'am' or 'pm' based on the given time
am
or pm
%M
Two digit representation of the minute
00
to 59
%S
Two digit representation of the second
00
to 59
%s
Unix Epoch Time timestamp
1612328167
{{ order.created_at | date: "%Y-%m-%d %H:%M:%S" }}
2021-02-03 04:56:07
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.
{{ "+1 800 444 4444" | e164 }}
=> +18004444444
{{ "+44 20 8743 8000" | e164 }}
=> +442087438000
{{ "020 8743 8000" | e164: "GB" }}
=> +442087438000
{{ "7325 7731" | e164: "HKG" }}
=> +85273257731
To format a phone number associated with a shipping address:
{{ order.shipping_address.phone | e164: order.shipping_address.country_code }}
iso3166_alpha3 ⭐
Converts a two-letter ISO 3166 country code to its three-letter equivalent.
{{ "US" | iso3166_alpha3 }}
=> USA
To format a shipping address country as an ISO 3166 alpha-3 country code:
{{ order.shipping_address.country_code | iso3166_alpha3 }}
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.
{{ order.transacations | json: "pretty" }}
[
{
"id": 5689665290299,
"order_id": 4592641409083,
"kind": "sale",
"gateway": "bogus",
"status": "success",
"message": "Bogus Gateway: Forced success",
"created_at": "2022-10-06T10:09:20+02:00",
"test": true,
"authorization": "53433",
"location_id": null,
"user_id": null,
"parent_id": null,
"processed_at": "2022-10-06T10:09:20+02:00",
"device_id": null,
"error_code": null,
"source_name": "web",
"payment_details": {
"credit_card_bin": "1",
"avs_result_code": null,
"cvv_result_code": null,
"credit_card_number": "•••• •••• •••• 1",
"credit_card_company": "Bogus"
},
"receipt": {
"paid_amount": "23.00"
},
"amount": "23.00",
"currency": "USD",
"admin_graphql_api_id": "gid://shopify/OrderTransaction/5689665290299"
}
]
json_parse, parse_json⭐
Allows parsing string in a format compliant with JSON file requirements into an object. It enables access to individual fields within the object using dot notation.
{%- capture material %}
{"properties":{"name":"Cotton","color":"White","structure":"Woven"}}
{%- endcapture %}
{%- assign material = material | json_parse %}
material: {{ material }}
material_json: {{ material | json }}
material_name: {{ material.properties.name }}
material: [object Object]
material_json: {"properties":{"name":"Cotton","color":"White","structure":"Woven"}}
material_name: Cotton
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.
{{ "now" | moment: "YYYY-MM-DD HH:mm:ss", "America/Chicago" }}
=> 2024-03-13 14:55:00
format
description
example value
DD
Two-digit day of the month (with leading zeros)
01
to 31
MM
Two digit representation of the month
01
to 12
YY
Two digit representation of the year
21
YYYY
Four digit representation for the year
2021
HH
Two digit representation of the hour in 24-hour format
00
to 23
hh
Two digit representation of the hour in 12-hour format
01
to 12
A
Upper-case 'AM' or 'PM' based on the given time
AM
or PM
a
Lower-case 'am' or 'pm' based on the given time
am
or pm
mm
Two digit representation of the minute
00
to 59
ss
Two digit representation of the second
00
to 59
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
You save {{ product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price }}%
abs
Returns the absolute value of a number.
{{ -25 | abs }}
25
abs
will also work on a string if the string only contains a number.
{{ "-19.86" | abs }}
19.86
at_most
Limits a number to a maximum value.
{{ 4 | at_most: 5 }}
{{ 4 | at_most: 3 }}
4
3
at_least
Limits a number to a minimum value.
{{ 4 | at_least: 5 }}
{{ 4 | at_least: 3 }}
5
4
ceil
Rounds an output up to the nearest integer.
{{ 1.2 | ceil }}
{{ 3.0 | ceil }}
{{ 3.45 | ceil }}
2
3
4
Liquid tries to convert the input to a number before the filter is applied.
{{ "4.5" | ceil }}
5
divided_by
Divides an output by a number. The output is rounded down to the nearest integer.
<!-- product.price = 200 -->
{{ product.price | divided_by: 10 }}
20
floor
Rounds an output down to the nearest integer.
{{ 4.6 | floor }}
{{ 4.3 | floor }}
4
4
minus
Subtracts a number from an output.
<!-- product.price = 200 -->
{{ product.price | minus: 15 }}
185
modulo
Divides an output by a number and returns the remainder.
{{ 12 | modulo: 5 }}
2
plus
Adds a number to an output.
<!-- product.price = 200 -->
{{ product.price | plus: 15 }}
215
round
Rounds the output to the nearest integer or specified number of decimals.
{{ 4.6 | round }}
{{ 4.3 | round }}
{{ 4.5612 | round: 2 }}
5
4
4.56
times
Multiplies an output by a number.
<!-- product.price = 200 -->
{{ product.price | times: 1.15 }}
230
to_fixed
Convert a number into a string, rounding the number to keep only the given number of decimals.
{{ 1.2345 | to_fixed: 2 }}
1.23
The difference between round
and to_fixed
it that round
returns a number so it trims trailing zeros while to_fixed
will preserve them.
{{ 4.5000 | round: 2 }}
{{ 4.5000 | to_fixed: 2 }}
4.5
4.50
String filters
String filters are used to manipulate outputs and variables of the string type.
append
Appends characters to a string.
{% assign filename = "/index.html" %}
{{ "website.com" | append: filename }}
website.com/index.html
base64_decode
Decodes a string to Base64 format
{{ 'b25lIHR3byB0aHJlZQ==' | base64_decode }}
one two three
base64_encode
Encodes a string to Base64 format
{{ 'one two three' | base64_decode }}
b25lIHR3byB0aHJlZQ==
capitalize
Capitalizes the first word in a string
{{ "title" | capitalize }}
Title
downcase
Converts a string into lowercase.
{{ 'UPPERCASE' | downcase }}
uppercase
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.
{{ "<p>test</p>" | escape }}
<p>test</p>
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).
{%- capture note -%}
Foo 123 Bar 456
{%- endcapture -%}
{{ note | extract_number }}
123
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.
{%- capture note -%}
Foo 012
Bar 3.4
Baz -56
{%- endcapture -%}
{{ note | extract_numbers | join: " " }}
012 3.4 -56
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.
{{ output | strip_all | md5 }}
newline_to_br
Inserts a <br > linebreak HTML tag in front of each line break in a string.
{% capture var %}
One
Two
Three
{% endcapture %}
{{ var | newline_to_br }}
<br />One<br />Two<br />Three<br />
prepend
Adds the specified string to the beginning of another string.
{{ 'sale' | prepend: 'Made a great ' }}
Made a great sale
remove
Removes all occurrences of a substring from a string.
{{ "Hello, world. Goodbye, world." | remove: "world" }}
Hello, . Goodbye, .
remove_first
Removes only the first occurrence of a substring from a string.
{{ "Hello, world. Goodbye, world." | remove_first: "world" }}
Hello, . Goodbye, world.
replace
Replaces all occurrences of a string with a substring.
<!-- product.title = "Awesome Shoes" -->
{{ product.title | replace: 'Awesome', 'Mega' }}
Mega Shoes
replace_first
Replaces the first occurrence of a string with a substring.
<!-- product.title = "Awesome Awesome Shoes" -->
{{ product.title | replace_first: 'Awesome', 'Mega' }}
Mega Awesome Shoes
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.
{{ "hello" | slice: 0 }}
{{ "hello" | slice: 1 }}
{{ "hello" | slice: 1, 3 }}
h
e
ell
If the passed index is negative, it is counted from the end of the string.
{{ "hello" | slice: -3, 2 }}
ll
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.
{% assign words = "Hi, how are you today?" | split: ' ' %}
{%- for word in words -%}
{{ word }}
{% endfor %}
Hi,
how
are
you
today?
strip
Strips tabs, spaces, and newlines (all whitespace) from the left and right side of a string.
{{ ' too many spaces ' | strip }}
too many spaces
lstrip
Strips tabs, spaces, and newlines (all whitespace) from the left side of a string.
{{ ' too many spaces ' | lstrip }}!
too many spaces !
rstrip
Strips tabs, spaces, and newlines (all whitespace) from the right side of a string.
{{ ' too many spaces ' | rstrip }}!
too many spaces!
strip_all ⭐
Strips tabs, spaces, and newlines (all whitespace) from the entire string.
{%- capture note -%}
Line 1
Line 2
Line 3
{%- endcapture -%}
{{ note | strip_all }}
Line 1Line 2Line 3
strip_html
Strips all HTML tags from a string.
{{ "<h1>Hello</h1> World" | strip_html }}
Hello World
strip_newlines
Removes any line breaks/newlines from a string.
{%- capture string_with_newlines -%}
Hello
there
{%- endcapture -%}
{{ string_with_newlines | strip_newlines }}
Hellothere
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.
{{ "The cat came back the very next day" | truncate: 13 }}
The cat ca...
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.
{{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | truncate: 18, ", and so on" }}
ABCDEFG, and so on
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:
{{ "I'm a little teapot, short and stout." | truncate: 15, "" }}
I'm a little te
truncatewords
Truncates a string down to the number of words passed as the first parameter. An ellipsis (...) is appended to the truncated string.
{{ "The cat came back the very next day" | truncatewords: 4 }}
The cat came back...
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.
{{ "The cat came back the very next day" | truncatewords: 4, "--" }}
The cat came back--
No ellipsis
You can avoid showing trailing characters by passing a blank string as the second parameter:
{{ "The cat came back the very next day" | truncatewords: 4, "" }}
The cat came back
upcase
Converts a string into uppercase.
{{ 'i want this to be uppercase' | upcase }}
I WANT THIS TO BE UPPERCASE
url_decode
Decodes a string that has been encoded as a URL or by url_encode.
{{ "%27Stop%21%27+said+Fred" | url_decode }}
'Stop!' said Fred
url_encode
Converts any URL-unsafe characters in a string into percent-encoded characters.
{{ "[email protected]" | url_encode }}
john%40liquid.com
Note that url_encode
will turn a space into a +
sign instead of a percent-encoded character.
{{ "Tetsuro Takara" | url_encode }}
Tetsuro+Takara
xml_escape
Replaces characters that are special characters in XML documents.
Char
Escape String
<
<
>
>
"
"
'
'
&
&
{{ "Jane & Joe O'Neill" | xml_escape }}
Jane & Joe O'Neill
Last updated
Was this helpful?