Swell Liquid Reference | Swell Developer Center

18 min read Original article ↗

Filters are used to modify Liquid output.

Array

Array filters are used to modify arrays.


compact

Removes empty values from an array.

concat

Combines two arrays by concatenating them.

{{ array1 | concat: array2 }}

find

Returns the first object in an array for which the queried attribute has the given value or nil if no match.

{{ array | find: 'property', 'value' }}

find_exp

Returns the first object in an array for which the given expression evaluates to true or nil if no item satisfies the expression.

{{ array | find_exp: 'item', 'item.property == value' }}

find_index

Returns the 0-based index of the first object in an array for which the queried attribute has the given value or nil if no match.

{{ array | find_index: 'property', 'value' }}

find_index_exp

Returns the 0-based index of the first object in an array for which the given expression evaluates to true or nil if no item satisfies the expression.

{{ array | find_index_exp: 'item', 'item.property == value' }}

first

Returns the first element of an array.

Dot notation

You can use the first filter with dot notation inside a tag.

{% assign first_item = array.first %}

group_by

Groups array items by a common attribute.

{{ array | group_by: 'property' }}

group_by_exp

The group_by_exp filter groups an array's items using a Liquid expression.

{{ array | group_by_exp: 'item', 'item.property == value' }}

has

Returns true if an item exists in an array for which the queried attribute has the given value or false otherwise.

{{ array | has: 'property', 'value' }}

has_exp

Returns true if an item exists in an array for which the given expression evaluates to true or false if no item satisfies the expression.

{{ array | has_exp: 'item', 'item.property == value' }}

join

Joins elements of an array into a string with a separator.

You can also use a custom separator for joined items.

last

Returns the last element of an array.

Dot notation

You can use the last filter with dot notation inside a tag.

{% assign last_item = array.last %}

map

Creates an array of values by extracting the values of a named property from another object.

{{ array | map: 'property' }}

reject

Creates an array excluding objects with a given property value or excluding truthy values by default if no property is given.

{{ array | reject: 'property', 'value' }}

reject_exp

Creates an array excluding objects for which the given expression evaluates to true.

{{ array | reject_exp: 'item', 'item.property == value' }}

reverse

Reverses the order of an array.

size

Returns the number of items in an array.

Dot notation

You can use the size filter with dot notation inside a tag.

{% assign count = array.size %}

sort

Sorts elements of an array.

You can also sort the array by an item's property.

{{ array | sort: 'property' }}

sort_natural

Sorts elements of an array in a natural order.

{{ array | sort_natural }}

You can also sort the array by an item's property.

{{ array | sort_natural: 'property' }}

sum

Returns the sum of all elements in an array.

You can also specify a property to sum object values.

{{ array | sum: 'property' }}

uniq

Removes duplicate elements from an array.

where

Filters an array based on a matching attribute value.

{{ array | where: 'property', 'value' }}

Asset

Filters used to retrieve theme or object related URLs for rendering.


asset_url

Generates a URL for a theme asset file, such as scripts, or stylesheets. It takes a filename and returns the full path to the asset hosted on the store's CDN.

If you're referencing an image asset, use asset_img_url.

{{ 'main.js' | asset_url }}

asset_img_url

Generates a URL for an image file stored in the theme's assets directory. It accepts optional parameters like size to specify image dimensions or formats, returning the full CDN-hosted path.

{{ 'image.png' | asset_img_url }}

Image sizes

You may pass a size parameter to resize the image accordingly. Specify either [width]x, x[height], or both [width]x[height]. You may also specify one of the named sizes below.

  • pico: 16x16 px
  • icon: 32x32 px
  • thumb: 50x50 px
  • small: 100x100 px
  • compact: 160x160 px
  • medium: 240x240 px
  • large: 480x480 px
  • grande: 600x600 px
  • original or master: 1024x1024 px
{{ 'image.png' | asset_img_url: 'large' }}

{{ 'image.png' | asset_img_url: 'x480' }}

{{ 'image.png' | asset_img_url: '480x' }}

{{ 'image.png' | asset_img_url: '480x480' }}

image_tag

Generates an HTML <img> tag for an image, using its URL or asset path. It supports optional parameters like alt, class, or width/height for customizing the tag's attributes.

Commonly used in combination with image_url.

{{ product | image_url: width: 250 | image_tag: attribute: 'value' }}

image_url

Returns the URL for an image, either from an object such as product or file. It can accept width and height parameter to specify its size.

{{ product | image_url: width: 250, height: 350 }}

Country flags

You may pass a string representing a locale and this filter will return an SVG representing that country's flag.

{{ store.locale | image_url }}

Cart

Cart filters are used to retrieve or modify details of a shopping cart.


item_count_for_variant

Returns the total quantity of a specific product variant in the cart. It takes a variant ID as input and outputs the sum of quantities across all matching cart line items.

{{ cart | item_count_for_variant: variant_id }}

Color

Color filters are used to mofy and output color values for CSS styling.


brightness_difference

Calculates the brightness difference between two colors, returning a value between 0 and 255. It takes two color values (e.g., hex, RGB) as inputs.

{{ '#57349B' | brightness_difference: '#FFE5F8' }}

color_brightness

Returns the perceived brightness of a color on a scale from 0 (black) to 255 (white). It accepts a color value like hex or RGB.

{{ '#57349B' | color_brightness }}

color_contrast

Calculates the contrast ratio between two colors, returning a value between 1 and 21. It takes two color values as inputs, useful for accessibility checks.

{{ '#57349B' | color_contrast: '#FFE5F8' }}

color_darken

Darkens a color by a specified percentage (0 to 100). It takes a color value and a percentage as inputs, returning the darkened color in the same format.

{{ '#57349B' | color_darken: 25 }}

color_desaturate

Reduces a color’s saturation by a specified percentage (0 to 100). It takes a color value and a percentage, returning the desaturated color.

{{ '#57349B' | color_desaturate: 25 }}

color_difference

Calculates the perceptual difference between two colors using the CIEDE2000 algorithm, returning a value between 0 and 100. It takes two color values as inputs.

{{ '#57349B' | color_difference: '#FFE5F8' }}

color_extract

Extracts a specific component (e.g., red, green, blue, hue, saturation, lightness) from a color. It takes a color value and the component name as inputs.

{{ '#57349B' | color_extract: 'red' }}

Color components

  • alpha
  • red
  • green
  • blue
  • hue
  • saturation
  • lightness

color_lighten

Lightens a color by a specified percentage (0 to 100). It takes a color value and a percentage, returning the lightened color in the same format.

{{ '#57349B' | color_lighten: 25 }}

color_mix

Blends two colors by a specified percentage (0 to 100). It takes two color values and a percentage, returning the mixed color.

{{ '#57349B' | color_mix: '#FFE5F8', 50 }}

If one color has an alpha component and the other does not, the alpha component of 1.0 will be assumed for the color without alpha.

{{ 'rgba(87, 52, 155, 0.50)' | color_mix: '#FFE5F8', 50 }}

color_modify

Adjusts a specific color component (e.g., red, hue, lightness) by a given value. It takes a color, component name, and adjustment value as inputs.

{{ '#57349B' | color_modify: 'blue', 255 }}

Color components

  • red, green, blue: An integer between 0 and 255
  • alpha: A decimal between 0 and 1
  • hue: An integer between 0 and 360
  • saturation, lightness: An integer between 0 and 100

color_saturate

Increases a color’s saturation by a specified percentage (0 to 100). It takes a color value and a percentage, returning the saturated color.

{{ '#57349B' | color_saturate: 25 }}

color_to_hex

Converts a color (e.g., RGB, HSL) to its hexadecimal representation. It takes a color value and returns a hex string (e.g., #FFFFFF).

{{ 'rgb(87, 52, 155)' | color_to_hex }}

color_to_hsl

Converts a color (e.g., hex, RGB) to its HSL representation. It takes a color value and returns a string like hsl(360, 100%, 50%).

{{ '#57349B' | color_to_hsl }}

color_to_oklch

Converts a color to the OKLCH color space, which is perceptually uniform. It takes a color value and returns a string like oklch(0.5, 0.2, 360).

{{ '#57349B' | color_to_oklch }}

color_to_rgb

Converts a color (e.g., hex, HSL) to its RGB representation. It takes a color value and returns a string like rgb(255, 255, 255).

{{ '#57349B' | color_to_rgb }}

hex_to_rgba

Converts a hex color to its RGBA representation, with an optional alpha channel value (0 to 1). It takes a hex color and an optional alpha value, returning a string like rgba(255, 255, 255, 1).

{{ '#57349B' | hex_to_rgba: 0.5 }}

Default

Default filters provide fallback values and HTML markup.


default

Returns a fallback value for a variable if it is nil, empty, or false. It takes a variable and a default value as inputs, returning the default if the variable is not set.

{{ product | image_url | default: 'product-default.png' }}

default_errors

Formats validation errors for a form field into a human-readable string. It takes a form object and a field name, returning error messages or an empty string if none exist.

{{ form.errors | default_errors }}

default_pagination

Generates HTML pagination links for a paginated collection. It takes a paginate object and returns navigation links with customizable styling.

{{ paginate | default_pagination }}

Next and previous labels

You can specify text labels for Next and Previous links to be generated by this filter.

{{ paginate | default_pagination: next: 'Next', previous: 'Previous' }}

Font

Font filters are used to output and modify font objects that are defined in font theme settings.

font_face

Generates a CSS @font-face rule for a font asset, specifying its source and properties. It takes a font file name and optional parameters like font-weight or font-style, returning the complete CSS rule.

{{ settings.header_font | font_face }}

You may specify the font display property of the @font-face declaration.

{{ settings.header_font | font_face: font_display: 'swap' }}

font_modify

Adjusts a font’s properties (e.g., weight, style) for use in a theme. It takes a font object or name and a property to modify, returning the updated font definition.

{% assign bold_font = settings.body_font | font_modify: 'weight', 'bold' %}

h1 { font-weight: {{ bold_font.weight }}; }

Font properties

  • style
    • normal: The normal variant of the same weight, if it exists.
    • italic: The italic variant of the same weight, if it exists.
    • oblique: The oblique variant of the same weight, if it exists. Some fonts support only italic or oblique variants, but not both.
  • weight
    • normal: 400 weight of the same style, if it exists.
    • bold: 700 weight of the same style, if it exists.
    • lighter: The lighter weight of the same style using the CSS font-weight property, if it exists.
    • bolder: The bolder weight of the same style using the CSS font-weight property, if it exists.
    • 100900: Specified weight of the same style, if it exists.
    • +100+900: Incremented weight by the given amount of the same style, if it exists.
    • -100-900: Decremented weight by the given amount of the same style, if it exists.

font_url

The font_url filter generates a URL for a font theme setting.

{{ settings.header_font | font_url }}

Format

Filters used to format dates and data into human or machine readable formats.


date

Formats a date or timestamp into a specified string format. It takes a date input (e.g., string, variable) and a format string, returning the formatted date based on Liquid’s date formatting syntax.

{{ blog.date_created | date: '%B %d, %Y' }}

Current date

You may specify the string 'now' or 'today' to render the current date.

{{ 'now' | date: '%B %d, %Y' }}

Date formatting

The format argument primarily conforms to Ruby's Format for Dates and Times. See to the LiquidJS documentation for details regarding the differences.

You may specify locale-aware formats using the following identifiers.

  • abbreviated_date
  • basic
  • date
  • date_at_time
  • default
  • on_date
  • short (deprecated)
  • long (deprecated)
{{ blog.date_created | date: format: 'on_date' }}

json

Converts a Liquid object (e.g., array, hash, or variable) into a JSON string. It takes the object as input and returns a compact JSON representation suitable for scripts or APIs.

json_pretty

Converts a Liquid object into a JSON string with formatted, human-readable indentation. It takes the object as input and returns the formatted JSON string.

{{ product | json_pretty }}

structured_data

Generates structured data markup (e.g., JSON-LD) for SEO purposes, based on an object like a product or blog. It takes the object and optional parameters to customize the schema output.

Product objects are rendered as a schema.org Product or ProductGroup.

Blog objects are rendered as a schema.org Article.

<script type="application/ld+json">
  {{ product | structured_data }}
</script>

HTML

HTML filters help render assets and properties using standard HTML elements and attributes.


inline_asset_content

Retrieves the content of an asset file (e.g., CSS, JS, or text) and outputs it directly in the template. It takes the asset file name as input and returns the file’s raw content.

{{ 'logo.svg' | inline_asset_content }}

placeholder_svg_tag

Generates an HTML <svg> tag for a placeholder image, typically used for lazy-loaded images. It takes a placeholder type (e.g., product, category) and optional attributes like class or width.

{{ 'product-1' | placeholder_svg_tag }}

The following placeholder names are used for compatibility with Shopify.

Outline illustrations

  • product-1
  • product-2
  • product-3
  • product-4
  • product-5
  • product-6
  • collection-1
  • collection-2
  • collection-3
  • collection-4
  • collection-5
  • collection-6
  • lifestyle-1
  • lifestyle-2
  • image

Color illustrations

  • product-apparel-1
  • product-apparel-2
  • product-apparel-3
  • product-apparel-4
  • collection-apparel-1
  • collection-apparel-2
  • collection-apparel-3
  • collection-apparel-4
  • hero-apparel-1
  • hero-apparel-2
  • hero-apparel-3
  • blog-apparel-1
  • blog-apparel-2
  • blog-apparel-3
  • detailed-apparel-1

script_tag

Generates an HTML <script> tag for a JavaScript file from the theme’s assets. It takes the asset file name and optional attributes like defer or async, returning the complete tag.

{{ 'main.js' | asset_url | script_tag }}

stylesheet_tag

Generates an HTML <link> tag for a CSS file from the theme’s assets. It takes the asset file name and optional attributes like media, returning the complete tag.

{{ 'main.css' | asset_url | stylesheet_tag }}

time_tag

Generates an HTML <time> tag with a formatted date or timestamp. It takes a date input and optional format string, returning the tag with a datetime attribute and formatted content.

This filter accepts the same formatting objects as the date filter.

{{ blog.date_created | time_tag }}

Localization

Used to translate text and format object output according to a customer's preferred locale or currency.


format_address

Formats an address object into a standardized, multi-line string. It takes an address object as input and returns a properly formatted address with fields like street, city, and country.

{{ account.shipping | format_address }}

translate

Retrieves the translated text for a given key from the theme's translation files. It takes a translation key and optional locale or parameters, returning the localized string.

Math

Math filters are used to perform mathematical operations on numbers.


abs

Returns the absolute value of a number. It takes a number as input and returns its non-negative value, removing any negative sign.

{{ -10 | abs }} # returns 10

at_least

Ensures a number is not less than a specified minimum value. It takes a number and a minimum value, returning the greater of the two.

{{ 2 | at_least: 4 }} # returns 4

{{ 6 | at_least: 4 }} # returns 6

at_most

Ensures a number is not greater than a specified maximum value. It takes a number and a maximum value, returning the lesser of the two.

{{ 5 | at_most: 4 }} # returns 4

{{ 2 | at_most: 4 }} # returns 2

ceil

Rounds a number up to the nearest integer. It takes a number as input and returns the smallest integer greater than or equal to it.

{{ 2.3 | ceil }} # returns 3

divided_by

Divides a number by another number. It takes a number and a divisor, returning the quotient as a float or integer depending on inputs.

{{ 10 | divided_by: 2 }} # returns 5

{{ 10 | divided_by: 1.2 }} # returns 8.3333333333

floor

Rounds a number down to the nearest integer. It takes a number as input and returns the largest integer less than or equal to it.

{{ 2.3 | floor }} # returns 2

minus

Subtracts one number from another. It takes a number and a value to subtract, returning the difference.

{{ 10 | minus: 2 }} # returns 8

modulo

Returns the remainder of a number divided by another. It takes a number and a divisor, returning the remainder of the division.

{{ 5 | modulo: 2 }} # returns 1

plus

Adds one number to another. It takes a number and a value to add, returning the sum.

{{ 1 | plus: 1 }} # returns 2

round

Rounds a number to a specified number of decimal places. It takes a number and an optional precision value (default: 0), returning the rounded number.

{{ 1.6 | round }} # returns 2

{{ 2.1 | round }} # returns 2

Rounding decimal places

You may specify a number of decimals to round to. If omitted, the filter rounds to the nearest integer.

{{ 1.234 | round: 2 }} # returns 1.23

times

Multiplies a number by another number. It takes a number and a multiplier, returning the product.

{{ 3 | times: 3 }} # returns 9

Money

Filters that are used to render currency values in different ways.


money

Formats a number as a currency string using the store’s default currency and settings. Takes a number as input and returns a formatted string, e.g., $12.99.

{{ product.price | money }}

money_with_currency

Formats a number as a currency string, including the currency code or symbol. Takes a number and optional currency code, returning a string like $12.99 USD.

{{ product.price | money_with_currency }}

money_without_currency

Formats a number as a currency string without the currency code or symbol. Takes a number and returns a string like 12.99.

{{ product.price | money_without_currency }}

money_without_trailing_zeros

Formats a number as a currency string, removing trailing zeros after the decimal point. Takes a number and returns a string like $12 for whole amounts or $12.5 for non-zero decimals.

{{ product.price | money_without_trailing_zeros }}

String

Filters used to modify strings.


append

Concatenates a string with another string. Takes a string as input and returns the combined result.

{{ request.origin | append: '/products' }}

capitalize

Capitalizes the first character of a string and converts the rest to lowercase. Takes a string and returns the capitalized version.

{{ 'this is a capitalized sentence.' | capitalize }}

downcase

Converts a string to lowercase. Takes a string and returns the lowercase result.

{{ product.name | downcase }}

escape

Escapes HTML special characters in a string (e.g., < to &lt;). Takes a string and returns the escaped version.

{{ '<p>This will be escaped.</p>' | escape }}

escape_once

Escapes HTML special characters in a string but does not double-escape already escaped characters. Takes a string and returns the escaped version.

{{ "&lt;p&gt;This is already escaped.&lt;/p&gt;" | escape }}

handleize

Converts a string to a hyphenated lowercase handle, often used in URLs.

{{ product.name | handleize }} # My Product => my-product

lstrip

Removes leading whitespace from a string. Takes a string and returns it with no whitespace at the start.

{{ "    Space on the left will be stripped." | lstrip }}

newline_to_br

Replaces newline characters (\n) with HTML <br> tags. Takes a string and returns it with newlines converted.

{{ product.description | newline_to_br }}

prepend

Prefixes a string with another string. Takes a string as input and returns the combined result.

{{ '/products' | prepend: request.origin }}

remove

Removes all occurrences of a substring from a string. Takes a string and substring, returning the string with all matches removed.

{{ 'one two three' | remove: 'three' }}

remove_first

Removes the first occurrence of a substring from a string. Takes a string and substring, returning the string with the first match removed.

{{ 'There is a very nice piece in a very beautiful place.' | remove_first: 'very' }}

remove_last

Removes the last occurrence of a substring from a string. Takes a string and substring, returning the string with the last match removed.

{{ 'There is a very nice piece in a very beautiful place.' | remove_last: 'very' }}

replace

Replaces all occurrences of a substring with another string. Takes a string, the substring to replace, and the replacement string, returning the modified string.

{{ product.slug | replace: '-', ' ' }}

replace_first

Replaces the first occurrence of a substring with another string. Takes a string, the substring to replace, and the replacement string, returning the modified string.

{{ product.slug | replace_first: '-', ' ' }}

replace_last

Replaces the last occurrence of a substring with another string. Takes a string, the substring to replace, and the replacement string, returning the modified string.

{{ product.slug | replace_last: '-', ' ' }}

rstrip

Removes trailing whitespace from a string. Takes a string and returns it with no whitespace at the end.

{{ "Space on the right will be stripped.    " | rstrip }}

slice

Extracts a substring from a string based on index and optional length. Takes a string, start index, and optional length, returning the extracted portion.

{{ '/products' | slice: 1 }} # 'products'

{{ '/products' | slice: 0, 1 }} # '/'

split

Divides a string into an array based on a delimiter. Takes a string and delimiter, returning an array of substrings.

{{ 'one two three' | split: ' ' }}

strip

Removes leading and trailing whitespace from a string. Takes a string and returns it with whitespace removed from both ends.

{{ "    Space on both sides will be stripped.    " | strip }}

strip_html

Removes HTML tags from a string, leaving only the text content. Takes a string and returns it without HTML tags.

{{ product.description | strip_html }}

strip_newlines

Removes all newline characters (\n) from a string. Takes a string and returns it with newlines removed.

{{ product.description | strip_newlines }}

truncate

Shortens a string to a specified length, appending an ellipsis (...) if truncated. Takes a string, length, and optional ellipsis string, returning the truncated result.

{{ product.description | truncate: 40 }}

A second parameter can be used to specify a custom ellipsis, or an empty string for no ellipsis.

{{ product.description | truncate: 40, '))' }}

{{ product.description | truncate: 40, '' }}

truncatewords

Shortens a string to a specified number of words, appending an ellipsis (...) if truncated. Takes a string, word count, and optional ellipsis string, returning the truncated result. Note: HTML tags are treated as words.

{{ product.description | strip_html | truncatewords: 40 }}

A second parameter can be used to specify a custom ellipsis, or an empty string for no ellipsis.

{{ product.description | strip_html | truncatewords: 40, '))' }}

{{ product.description | strip_html | truncatewords: 40, '' }}

upcase

Converts a string to uppercase. Takes a string and returns the uppercase result.

{{ product.name | upcase }}

url_decode

Decodes a URL-encoded string, converting percent-encoded characters back to their original form. Takes a string and returns the decoded version.

{{ 'user%40example.com' | url_decode }} # user@example.com

url_encode

Encodes a string for safe use in URLs, converting special characters to percent-encoded format. Takes a string and returns the encoded version.

{{ 'user@example.com' | url_decode }} # user%40example.com

url_escape

Escapes a string for use in URLs, similar to url_encode, ensuring special characters are properly encoded. Takes a string and returns the escaped version.

{{ '<p>This will become URL safe.</p>' | url_escape }}

Shopify compatibility

Filters implemented strictly to enable compatibility with Shopify themes.


shopify_asset_url

Generates a URL for a globally accessible Shopify asset.

  • option_selection.js
  • api.jquery.js
  • shopify_common.js
  • customer_area.js
  • currencies.js
  • customer.css
{{ 'option_selection.js' | shopify_asset_url }}