Liquid Filters
Use Liquid filters to modify the feed output of numbers, strings, variables and objects. Apply Liquid filter to Liquid objects by placing a pipe character |
after the Liquid object’s name and attribute {{ object.attribute | filter}}
, as shown:
<description>
{{ variant.body_html | strip_html | t | truncate: 5000, "" }}
</description>
String Filters
String filters are used to manipulate outputs and variables of the string type.
append
Appends characters to a string.
Example
{% assign text = "sales" %}
{{ text | append: '.jpg' }}
<!-- text = "sales" -->
sales.jpg
capitalize
Capitalizes the first letter of each word in a string.
Example
{% assign text = "capitalize me" %}
{{ text | capitalize }}
<!-- text = "capitalize me" -->
Capitalize Me
downcase
Brings each uppercase character in a string to lowercase.
Example
{{ product.title | downcase }}
<!-- product.title = "RED Jacket" -->
red jacket
escape
Replaces characters in a string with escape sequences so that the string can be used in a URL. Note: a browser will visually display escape symbols as HTML tags.
Example
{{ product.title | escape }}
<!-- product.title = "<p>Red Jacket</p>" -->
<p>Red Jacket</p>
escape_once
Replaces characters in a string with escape sequences without changing already existing escaped entities in this string.
Example
{{ 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 a string.
Example
{{ product.title | newline_to_br }}
<!-- product.title = "Gaming Marker
Maker
Starter
Kit" -->
Gaming Marker
<br/>
Maker
<br/>
Starter
<br/>
Kit
prepend
Prepends given characters to a string.
Example
{{ product.title | prepend: 'Bestseller ' }}
<!-- product.title = "Gaming Marker" -->
Bestseller Gaming Marker
remove
Removes every occurrence of a given substring.
Example
{{ product.title | remove: 'Holiday sale' }}
<!-- product.title = "Holiday sale Gaming Marker Holiday sale" -->
Gaming Marker
removefirst
Removes the first occurrence of a given substring.
Example
{{ product.title | remove: '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
{{ product.title | replace: 'Bestseller', 'Hot sales' }}
<!-- product.title = "Bestseller Gaming Marker" -->
Hot sales Gaming Marker
replace_first
Replaces the first occurrence of a given substring with another substring.
Example
{{product.title | replace_first: 'Bestseller', 'Hot sales' }}
<!-- product.title = "Bestseller Gaming Marker Bestseller" -->
Hot sales Gaming Marker Bestseller
singular
Converts a word to its singular form
Example
{{ product.collection_title | singular }}
<!-- product.collection_title = Jackets -->
Jacket
split
Splits a string by a given substring.
Example
{% assign title = variant.title | split: ' ' %}
{% for word in title %}
{{ word }} -
{% endfor %}
<!-- variant.title = Superhero toys bestseller -->
Superhero - toys - bestseller -
strip
Removes all whitespace (tabs, spaces, and newlines) from both the left and right sides of a string. It does not affect spaces between words.
Example
```xml title="INPUT"
{{ product.title | strip }}
```
```xml title="OUTPUT"
<!-- product.title = " Red Jacket " -->
Red Jacket
```
strip_html
Strips all HTML tags from a string.
Example
{{ product.body_html | strip_html }}
<!-- product.body_html = "<u>Red</u> Jacket" -->
Red Jacket
stripnewlines
Removes any line breaks/newlines from a string.
Example
{{ product.body_html | strip_newlines }}
<!-- product.title = Gaming
marker -->
Gaming marker
truncate
Truncates a string down to the given number of characters.
Example
The truncated string includes a ...
mark, which counts into the specified limit of included symbols.
{{ product.description | truncate: 20 }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller produc...
Avoid the occurrence of ...
at the end of the truncated string by using | truncate: '20',''
{{ product.description | truncate: '20','' }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller product d
truncatewords
Truncates a string down to the given number of words.
Example
If you don't need a ...
mark at the end of the truncated string use | truncatewords: '5',''
.
{{ product.description | truncatewords: 5 }}
<!-- product.description = "Bestseller product during the holiday season this year" -->
Bestseller product during the holiday...
upcase
Makes all characters of a string uppercase.
Example
{{ product.title | upcase }}
<!-- product.title = "gaming marker" -->
GAMING MARKER
Money filters
Money filters allow change format prices.
currency
Add a letter mark of a currency used in a store.
Example
{{ variant.price | currency }}
1.47 EUR
convert_to_currency
Generates the feed with a currency that is different from the store’s default currency. Mulwi Shopping Feeds utilizes currency exchange rates that Shopify provides. The feed can be converted only to currencies available in this list.
Example
{{ variant.price | convert_to_currency: 'USD' }}
<!-- currency = EUR -->
15.62 USD
money_without_currency
Formats the price with two decimals.
Example
Two decimals in price value are required by marketplaces, for example $1.99. Values like $1.501, which can result from math operations on price, are not allowed.
{{ product.price | money_without_currency }}
<!-- product.price = 21.12345 -->
21.12
Math filters
Math filters allow you to apply mathematical tasks. And, as with any other filters, they are applied in order of left to right.
ceil
Rounds an output up to the nearest integer.
Example
{% assign number = 4.3 %}
{{ number | ceil }}
<!-- number = 4.3 -->
5
divideby
Divides an output by a number. The result is rounded down to the nearest integer.
Example
{{ product.price | divided_by: 10 }}
<!-- product.price = 200 -->
20
floor
Rounds an output down to the nearest integer.
Example
{{ product.price | floor }}
<!-- product.price = 4.6 -->
4
minus
Subtracts a number from an output.
Example
{{ product.price | minus: 15 }}
<!-- product.price = 20 -->
5
modulo
Returns the remainder of a division operation.
Example
{{ product.price | modulo: 2 }}
<!-- product.price = 21 -->
1
round
Rounds the output to the nearest integer. In case you need to get the item price rounded to two decimals, use the money_without_currency
filter.
Example
{{ product.price | round }}
<!-- product.price = 2.3657 -->
2
times
Multiplies an output by a number.
Example
{{ product.price | times: 2.0 }}
<!-- product.price = 10 -->
20
Array filters
Array filters change the output of arrays.
first
Returns the first item of an array.
Example
{{ variant.tags | first }}
<!-- variant.tags = [shoes summer women] -->
shoes
index
Returns the specified index location in an array.
Example
{{ variant.tags[0] }}
<!-- variant.tags = [apparel accessories women] -->
apparel
join
Generates a string by joining the elements of an array with the specified character.
Example
{{ variant.tags | join: ', ' }}
<!-- variant.tags = [summer beach accessories] -->
summer, beach, accessories
last
Returns the last element of an array.
Example
{{ variant.tags | last }}
<!-- variant.tags = [summer beach accessories] -->
accessories
map
Accepts an attribute of an array element as a parameter. Then creates an array from each element's value of the given array.
Example
<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 array elements order.
Example
{{ product.tags | reverse }}
<!-- product.tags = [toys summer beach] -->
[beach summer toys]
The | reverse
filter can be used with strings. First use | split: ''
filter to split the string, and when the generated array is reversed - apply | join: ''
filter to generate a string from the array.
{{ product.title | split: '' | reverse | join: '' }}
<!-- product.title = "Gaming" -->
gnimaG
size
Returns the number of array elements.
Example
{{ product.tags | size }}
<!-- product.tags = [shoes clothes toys] -->
3
Additionally, | size
can return the number of characters in a string.
{{ product.title | size }}
<!-- product.title = Gaming marker -->
13
sort
Sorts the elements of an array by a given attribute of an element in the array.
Example
{% assign prices = '67, 12, 99, 23, 56' | split: ',' %}
{{ prices | sort }}
[12 23 56 99 67]
where
Creates an array that includes only the objects with a given property value
Example
{% for product in context.products %}
<![CDATA[{{ product.variants | where: "in_stock" | map: 'option1' | remove_empty | uniq | join: ", " }}]]>
{% endfor %}
black, yellow, blue, white
Alternatively to using where
the array of objects with a given property value can be obtained using if
operator:
{% 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
General filters serve many different purposes like set format, translations.
date
Displays a timestamp and converts it into another date format. Here are some format directives:
- %a - the abbreviated day of the week (Mon)
- %b - the abbreviated month name (Jan)
- %^b - uppercased (JAN)
- %d - day of the month, zero-padded (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)
Refer to Ruby documentation for date format directives.
Example
{{ 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
{{ price | default: 1.99 }}
<!-- price = "" -->
1.99
debug
Use it to inspect a Liquid object.
Example
{{ product.title | debug }}
<!-- product.title = "Toy Mobile Command Center" -->
debug(Toy Mobile Command Center)
t
Translates text to another language. Change the language for translation in the feed settings.
Example
{{ product.body_html | t }}
<!-- product.body_html = "Red Jacket" -->
Chaqueta Roja
img_url
Resize image
Example
you can specify exact dimensions:
{{ product.image_url | img_url: "300x300" }}
or you can specify only a width or only a height:
{{ product.image_url | img_url: "600x" }}
{{ product.image_url | img_url: "x300" }}