Modifiers (Liquid Filters)
Liquid filters (also simply known as modifiers) are functions, written in the Shopify Liquid template language, that change the output of numbers, strings, variables, and Liquid objects.
To apply a Liquid filter to a Liquid object, place a pipe character |
after the object and its attribute, and then specify the necessary filter: {{ object.attribute | filter }}
You can consequenty apply several Liquid filters to one variable: {{ object.attribute | filter1 | filter2 }}
.
The filters are applied from left to right.
Example
Here's how you would apply truncation and capitalization filters to the product's title:
{{ product.title | truncate: 13, '' | capitalize }}
<!-- Original title: "gaming marker Maker Starter Kit" -->
Gaming Marker
String Filters
String filters change the outputs and variables of the string type.
append
Appends characters to the string.
Example of using append
{% assign text = "sales_banner" %}
{{ text | append: '.jpg' }}
sales_banner.jpg
capitalize
Capitalizes the first letter of each word in the string.
Example of using capitalize
{% assign text = "capitalize me" %}
{{ text | capitalize }}
Capitalize Me
downcase
Makes all characters of the string lowercase.
Example of using downcase
{{ product.title | downcase }}
<!-- product.title = "RED Jacket" -->
red jacket
escape
Replaces all special characters in the string with escape sequences so that the string can be used in a URL.
NOTE
The browser will visually display escape symbols as HTML tags.
Example of using espace
{{ product.title | escape }}
<!-- product.title = "<p>Red Jacket</p>" -->
<p>Red Jacket</p>
escape_once
Replaces special characters in the string with escape sequences, but without changing already existing escaped entities.
NOTE
The browser will visually display escape symbols as HTML tags.
Example of using escape_once
{{ product.title | escape_once }}
<!-- product.title = "<Red & Jacket<" -->
<Red & Jacket<
newline_to_br
Inserts a <br>
linebreak HTML tag in front of each line break in the string.
Example of using newline_to_br
{{ product.body_html | newline_to_br }}
<!-- product.body_html = "Gaming Marker
Maker
Starter
Kit" -->
Gaming Marker
<br/>
Maker
<br/>
Starter
<br/>
Kit
prepend
Prepends characters to the string.
Example of using prepend
{{ product.title | prepend: 'Bestseller ' }}
<!-- product.title = "Gaming Marker" -->
Bestseller Gaming Marker
remove
Removes all occurrences of a given substring from the string.
Example of using remove
{{ product.title | remove: 'Holiday sale' }}
<!-- product.title = "Holiday sale Gaming Marker Holiday sale" -->
Gaming Marker
removefirst
Removes the first occurrence of a given substring from the string.
Example of using removefirst
{{ product.title | removefirst: 'Holiday sale' }}
<!-- product.title = "Holiday sale Gaming Marker Holiday sale" -->
Gaming Marker Holiday sale
replace
Replaces all occurrences of a given substring with another substring.
Example of using replace
{{ product.title | replace: 'Hot Sale', 'Bestseller' }}
<!-- product.title = "Hot Sale Gaming Marker" -->
Bestseller Gaming Marker
replace_first
Replaces the first occurrence of a given substring with another substring.
Example of using replace_first
{{product.title | replace_first: 'Hot Sale', 'Bestseller' }}
<!-- product.title = "Hot Sale Gaming Marker Hot Sale" -->
Bestseller Gaming Marker Hot Sale
singular
Converts a plural word into its singular form.
Example of using singular
{{ product.product_type | singular }}
<!-- product.product_type = Jackets -->
Jacket
size
Returns the number of characters in the string.
Example of using size for strings
{{ product.title | size }}
<!-- product.title = "Gaming marker" -->
13
split
Splits the string by a given substring into an array of strings.
Example of using split
{% assign title = product.title | split: ' ' %}
{% for word in title %}
{{ word }} -
{% endfor %}
<!-- product.title = "Gaming Marker Maker Starter Kit" -->
Gaming - Marker - Maker - Starter - Kit -
strip
Removes all whitespace characters (tabs, spaces, and newlines) from the left and the right sides of the string. This filter does not change the whitespace characters between words.
Example of using strip
{{ product.title | strip }}
<!-- product.title = " Gaming Marker Maker Starter Kit " -->
Gaming Marker Maker Starter Kit
strip_emoji
Removes all emojis and special characters from the string.
Example of using strip_emoji
{{ product.body_html | strip_emoji }}
<!-- product.body_html = "Smoothie Maker To Go ✅🏆🍟" -->
Smoothie Maker To Go
strip_html
Removes all HTML tags from the string.
Example of using strip_html
{{ product.body_html | strip_html }}
<!-- product.body_html = "<u>Red</u> Jacket" -->
Red Jacket
strip_newlines
Removes all line breaks and newlines from the string.
Example of using strip_newlines
{{ product.body_html | strip_newlines }}
<!-- product.body_html = "Gaming
Marker
Maker" -->
Gaming Marker Maker
truncate
Truncates the string down to a given number of characters.
If the given number of characters is less than the string's length, an ellipsis ...
is appended to the truncated string and is included in the character count.
You can pass a different appended substring as an optional second argument.
Example of using truncate
Here is the default behaviour of this filter:
{{ product.description | truncate: 20 }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller produc...
To change the default ...
appended substring, pass the desired substring as a second parameter in such way:
{{ product.description | truncate: '20', 'ts!' }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller products!
To remove the appended substring altogether, pass an empty string ''
as a second parameter:
{{ product.description | truncate: '20', '' }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller product d
truncatewords
Truncates the string down to a given number of words.
If the given number of words is less than the string's total word count, an ellipsis ...
is appended to the truncated string.
You can pass a different appended substring as an optional second argument.
Example of using truncatewords
Here is the default behaviour of this filter:
{{ product.description | truncatewords: 5 }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller product during the holiday...
To remove the appended substring, pass an empty string ''
as a second parameter:
{{ product.description | truncatewords: 5, '' }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller product during the holiday
upcase
Makes all characters of the string uppercase.
Example of using upcase
{{ product.title | upcase }}
<!-- product.title = "gaming marker" -->
GAMING MARKER
Money filters
Money filters change the outputs of the prices.
currency
Adds the currency letter code.
Example of using currency
{{ variant.price | currency }}
1.47 EUR
convert_to_currency
Converts product prices from the store's default currency into a different one.
Mulwi can use currency exchange rates provided by either Shopify or European Central Bank. You can select which rates to use in the app's settings.
Example of using convert_to_currency
{{ variant.price | convert_to_currency: 'USD' }}
<!-- variant.price = 1.47 EUR -->
1.67 USD
money_without_currency
Formats the price with two decimal places (without rounding).
Example of using money_without_currency
{{ product.price | money_without_currency }}
<!-- product.price = 21.12345 -->
21.12
Math filters
Math filters apply mathematical operations to the outputs and variables of the numeric type.
ceil
Rounds the number up to the nearest integer.
Example of using ceil
{% assign number = 4.3 %}
{{ number | ceil }}
<!-- number = 4.3 -->
5
divided_by
Divides the number by a given value.
Example of using divided_by
{{ product.price | divided_by: 10 }}
<!-- product.price = 200 -->
20
floor
Rounds the number down to the nearest integer.
Example of using floor
{{ product.price | floor }}
<!-- product.price = 4.6 -->
4
minus
Subtracts a given value from the number.
Example of using minus
{{ product.price | minus: 15 }}
<!-- product.price = 20 -->
5
modulo
Returns the remainder of a division operation by a given value.
Example of using modulo
{{ product.price | modulo: 2 }}
<!-- product.price = 21 -->
1
round
Rounds the number to the nearest integer.
Example of using round
{{ product.price | round }}
<!-- product.price = 2.3657 -->
2
times
Multiplies the number by a given value.
Example of using time
{{ product.price | times: 2.5 }}
<!-- product.price = 10 -->
25
Array filters
Array filters change the outputs of arrays.
first
Returns the first item of the array.
Example of using first
{{ variant.tags | first }}
<!-- variant.tags = [shoes summer women] -->
shoes
index
Returns the item of the array located at a specified index. Indexing starts from 0.
Example of using index
{{ variant.tags[0] }}
<!-- variant.tags = [apparel accessories women] -->
apparel
join
Joins the items of the array into a string using the specified characters as a separator.
Example of using join
{{ variant.tags | join: ', ' }}
<!-- variant.tags = [summer beach accessories] -->
summer, beach, accessories
last
Returns the last element of the array.
Example of using last
{{ variant.tags | last }}
<!-- variant.tags = [summer beach accessories] -->
accessories
map
Extracts the given attribute from the array's items and returns a new array with the extracted values.
Example of using map
<products>
{% for product in context.products %}
{% assign variant_prices = product.variants | map: 'price' %}
<item>
{% for variant_price in variant_prices %}
<price>{{ variant_price }}</price>
{% endfor %}
</item>
{% endfor %}
</products>
<products>
<item>
<price>26</price>
</item>
<item>
<price>32</price>
</item>
</products>
reverse
Reverses the order of items in the array.
Example of using reverse
{{ product.tags | reverse }}
<!-- product.tags = [toys summer beach] -->
[beach summer toys]
size
Returns the number of items in the array.
Example of using size for array
{{ product.tags | size }}
<!-- product.tags = [shoes clothes toys] -->
3
sort
Sorts the items of the array by a given attribute of the array's item.
Example of using sort
{% assign prices = '67, 12, 99, 23, 56' | split: ',' %}
{{ prices | sort }}
[12 23 56 67 99]
where
Creates an array that includes only the objects with a given attribute value.
Example of using where
{% for product in context.products %}
<![CDATA[{{ product.variants | where: "in_stock" | map: 'option1' | join: ', ' }}]]>
{% endfor %}
black, yellow, blue, white
You can also achieve the same result, but without the filter, in such way:
{% for product in context.products %}
{% if product.title contains 'TOY' %}
<item>
<name>{{ product.title }}</name>
</item>
{% endif %}
{% endfor %}
<item>
<name>TOY Mobile Command Center</name>
</item>
<item>
<name>TOY Fun Family</name>
</item>
<item>
<name>TOY Juniors</name>
</item>
Additional filters
Additional filters serve other purposes that aren't exclusively related to a single data type.
date
Displays a timestamp into the given format.
Here are the most commonly used format directives:
- %a - the abbreviated day of the week (Mon)
- %b - the abbreviated month name (Jan)
- %^b - uppercased value (JAN)
- %d - zero-padded day of the month (01..31)
- %-d - no-padded day of the month (1..31)
- %y - year (00..99)
- %k - an hour of the day, 24-hour clock, blank-padded ( 0..23)
- %M - minute of the hour (00..59)
For more information about the date format directives, please refer to the official Ruby documentation.
Example of using date
{{ product.variant_created_at | date: "%a, %b %d, %y" }}
<!-- product.variant_created_at = "2022-04-25 16:15:22 +0300 EEST" -->
Mon, Apr 25, 22
default
Sets a default value for a variable with no assigned value.
Example of using default
{{ price | default: 1.99 }}
<!-- price = "" -->
1.99
debug
Prints out a Liquid object for you to inspect it.
Example of using debug
{{ product.title | debug }}
<!-- product.title = "Gaming Market Maker" -->
debug(Gaming Market Maker)
img_url
Resizes the image into the given dimensions.
Example of using img_url
<!-- exact dimensions -->
{{ product.image_url | img_url: "300x300" }}
<!-- changing only width -->
{{ product.image_url | img_url: "600x" }}
<!-- changing only height -->
{{ product.image_url | img_url: "x300" }}
metafield_tag
Converts a metafield's JSON object into an HTML string.
Example of using metafield_tag
{{ product.metafields.my_fields.rich_text_meta | metafield_tag }}
Some HTML text from rich_text_meta
option_by_name
Returns the value of the product variant's option with a given name.
Example of using option_by_name
{{ variant | option_by_name: 'Color' }}
Blue
t
Translates the text into another language.
For more information about changing the feed translation language, please visit the Translate the feed page.
Example of using t
{{ product.body_html | t }}
<!-- product.body_html = "Red Jacket" -->
Chaqueta Roja