Shopify Liquid filters: the ones you use every day, and how each one bites
Shopify's reference lists every filter alphabetically, which is no help when your price says 24 instead of 24.99. This one is sorted by what you're trying to do, with the behavior that catches people out.
Key takeaways
- Prices in Liquid are in subunits, and dividing by the integer 100 throws away the cents. Use a money filter and do any math before it.
defaultreplacesfalseas well as empty and nil, so a checkbox setting that defaults to on needsallow_false: true.- Conditions in
iftags don't take filters: assign the result to a variable first. truncatecounts its own ellipsis,capitalizelowercases everything after the first letter, andsortputs capitals before lowercase.img_urlandimg_tagare replaced byimage_urlandimage_tag, andimage_urlerrors without a width or height.
A product costs 24.99. Someone writes this, sees "24", and spends an hour checking the product in the admin:
{{ product.price | divided_by: 100 }} {%- comment -%} 24, the cents are gone {%- endcomment -%}
{{ product.price | divided_by: 100.0 }} {%- comment -%} 24.99, but no symbol or store format {%- endcomment -%}
{{ product.price | money }} {%- comment -%} what you wanted {%- endcomment -%}`divided_by` returns the type of its divisor, so an integer divisor gives an integer result, rounded down. Plenty of filter bugs are like that: the filter does what its page says, and the page isn't where you looked. The groups below follow the jobs you actually do in a section.
Three rules for every filter
Filters run left to right with no brackets and no precedence, so {{ 10 | plus: 2 | times: 3 }} is 36. If you need another order, write the chain in that order or split it across assign tags.
Conditions don't take filters. {% if product.title | size > 20 %} doesn't do what it looks like it does. Assign first, then compare:
{%- assign title_length = product.title | size -%}
{%- if title_length > 20 -%} ... {%- endif -%}Parameter values don't take filters either. Build the value with assign, then pass the variable.
Prices: the four money filters
All four take a price in the currency's subunit. The outputs below are the ones in Shopify's docs for an input of 1000.
| Filter | Output | Use it for |
|---|---|---|
money | $10.00 | Almost every price on the page |
money_with_currency | $10.00 CAD | Stores selling in several currencies with the same symbol |
money_without_currency | 10.00 | Rare: when you must place the symbol yourself |
money_without_trailing_zeros | $10 | Tidy marketing prices. 10.50 still prints as a normal money value |
Don't trust the names too far. money follows the store's HTML without currency format, and that format can contain HTML and use commas as decimal separators, all under the merchant's control in settings. That has two consequences:
- Do math before the money filter, while the value is still a number.
{{ variant.price | times: 3 | money }}is right. Doing arithmetic on the output ofmoney_without_currencyisn't, because "1,000.00" and "10,00" aren't numbers Liquid can read correctly. - A money string used anywhere HTML doesn't belong, such as an
<option>label or an attribute, should go throughstrip_html, andescapefor attributes, in case the store format includes a tag.
For a percentage saved, multiply before you divide. Dividing first gives 0 on integers:
{%- if product.compare_at_price > product.price -%}
{%- assign saved = product.compare_at_price | minus: product.price | times: 100 | divided_by: product.compare_at_price -%}
<span class="badge">-{{ saved }}%</span>
{%- endif -%}That result rounds down, so 19.9% shows as 19%, which is the honest direction for a claim about savings.
Text you print into markup
| Job | Filter | What catches people |
|---|---|---|
| Put a setting in an attribute | escape | Without it, a quote in the merchant's text closes your attribute. escape_once won't double-encode text that's already escaped. |
| Cut a description down | strip_html, then truncatewords: 30 | Reverse the order and you can cut through a tag and leave it open. |
| Cut by characters | truncate: 20 | The ellipsis counts toward the 20, so you get 17 characters and "...". Pass a second argument to change it. |
| Make an id or class from a name | handleize | "Blue Shirt!" becomes blue-shirt. Two names can collapse to one handle. |
| Put text in a URL | url_encode or url_param_escape | url_encode turns spaces into +. Both are for values, not whole URLs. |
| Fill a placeholder | replace: '[name]', customer.first_name | Replaces every instance. replace_first exists if you need just one. |
| Keep line breaks from a textarea | newline_to_br | Escape first, since this filter adds tags you want to keep. |
| Singular or plural | pluralize: 'item', 'items' | English-shaped. For a translated store, use t with locale keys. |
| Fix the case | capitalize | It lowercases everything after the first character, so "iPhone case" becomes "Iphone case". |
Assembling strings is also a filter job. append and prepend are how you build a scoped id, as in {% assign sid = 'box-' | append: section.id %}. For anything longer, capture is easier to read.
The default filter and your checkbox
`default` steps in when a value is empty, nil or false. That last one breaks checkbox logic quietly:
{{ section.settings.show_badge | default: true }} {%- comment -%} always true {%- endcomment -%}
{{ section.settings.show_badge | default: true, allow_false: true }} {%- comment -%} respects an unticked box {%- endcomment -%}Lists of products, tags and blocks
| Filter | Example | Remember |
|---|---|---|
where | collection.products | where: 'available' | With only a property name, it keeps items where that property is true. |
reject | products | reject: 'vendor', 'Acme' | The opposite of where. |
find | products | find: 'handle', 'tote' | Returns the first match, not an array. find_index gives its position. |
has | products | has: 'available' | Returns true or false, so assign it, then test the variable. |
map | products | map: 'vendor' | uniq | Pulls one property out. This pair is the usual way to list unique vendors. |
sum | cart.items | sum: 'quantity' | Works with a property name or directly on an array of numbers. |
sort | products | sort: 'price' | Case-sensitive for text: "Zebra" comes before "apple". Use sort_natural for names. |
split | 'S,M,L' | split: ',' | The usual way to make an array from nothing, since Liquid has no array literal. |
join | product.tags | join: ', ' | Defaults to a single space between items. |
compact | items | map: 'image' | compact | Drops nil values, which is handy right after map. |
One limit belongs here because it makes these filters look broken. collection.products only reaches the products on the current page of results, so where and map work on that window and not on the whole catalog. A filter can't see what the page never fetched.
Images and theme files
`image_url` gives a CDN URL and must have a width or height, otherwise it returns an error. `image_tag` turns that URL into an <img> with width, height, a srcset and alt text from the media. They almost always travel as a pair:
{{ product.featured_image | image_url: width: 800 | image_tag: sizes: '(min-width: 750px) 400px, 100vw' }}| Filter | For |
|---|---|
asset_url | A file in the theme's assets folder, usually piped into stylesheet_tag or script_tag |
inline_asset_content | Printing an asset's content into the page. It must be an SVG, JS or CSS file |
file_url | A file uploaded under Content › Files |
placeholder_svg_tag | A stand-in drawing for an empty image setting, as in 'product-1' | placeholder_svg_tag |
You'll still see img_url and img_tag in older themes and tutorials. Shopify's docs mark both as replaced, so new code uses the newer pair.
Colors and fonts from settings
color_modifychanges one channel:{{ section.settings.accent | color_modify: 'alpha', 0.2 }}makes a tint. The output format depends on what you changed, so modifying alpha on a hex value returnsrgba().color_lightenandcolor_darkentake a percentage from 0 to 100, which suits hover states.color_contrastreports the ratio between two colors, andcolor_brightnesstells you if a background is dark. Use one of them to choose readable text over a merchant's color.font_faceoutputs the@font-facerule for afont_pickersetting. Put it in a{% style %}tag withfont_display: 'swap'.font_modifymakes a bold or italic variant, and returns nil when that variant doesn't exist. Check for that, or the nextfont_faceprints nothing and your bold text falls back to a synthesized one.
Dates and translated strings
date takes strftime patterns, as in {{ article.created_at | date: '%B %d, %Y' }}, or a locale-aware preset with format: 'abbreviated_date'. Prefer the preset on translated stores, since a hand-written pattern keeps its own order in every language. Shopify's docs flag that 'now' | date shows when the Liquid was last rendered, which may be cached, so don't build a live countdown on it. The current time has to come from JavaScript. time_tag wraps a date in a proper <time> element.
`t` is the alias for translate and reads from your locale files. A section's schema can carry its own locales object, readable only by that section, which is the usual approach for a section meant to work in any theme. Words kept in settings don't need t: the merchant translates those with Shopify's translation tools.
When a filter seems to do nothing
Print what you have before you blame the filter: {{ thing | json }} shows you the value in full, and an empty result usually means the object was nil and not that the filter failed. A filter given the wrong type can give a blank instead of an error. If you get an actual error message on the page, our list of Shopify Liquid errors covers the common ones. For the loops these filters so often sit inside, see the Liquid for loop guide.
This list covers the filters that come up daily in sections. The complete set, with less common ones such as metafield_tag, structured_data, link_to_type and weight_with_unit, is in Shopify's filter reference.
Questions, answered
What are filters in Shopify Liquid?
Filters change the output of a variable or object. You write them after a pipe character inside an output tag or an assign, like {{ product.title | upcase }}, and you can chain several, running from left to right.
What is the difference between money and money_without_currency?
money prints the price using the store's HTML without currency format, usually with a symbol, such as $10.00. money_without_currency prints just 10.00. Neither output is safe for further math, so calculate first and format last.
Why does divided_by remove the decimals?
The result takes the type of the divisor. Divide by the integer 100 and you get an integer, rounded down. Divide by 100.0 and you get a float. For prices, use a money filter.
Can you use a filter inside an if statement in Liquid?
No. Assign the filtered value to a variable first, then compare the variable in your if tag.
Why is my default filter ignoring a false value?
default treats false like an empty value and replaces it. Add allow_false: true to keep a false value, which you need for checkbox settings.