Skip to documentation content

Utility Filters

Utility Filters

Liquid filters for encoding, decoding, and other utility operations.

default filter

If the input is null, an empty string, or invalid (object.is_valid == false), return the default value. Otherwise returns the input. Note that if the input is the boolean value false, this will return false.

default: object default
Example Use the default filter for empty or missing stringsOutput a default value if the input string is empty
Liquid
{%- var greeting = "" -%}
{{- greeting | default: "hello" }} world
{%- set greeting = "goodbye" -%}
{{- greeting | default: "hello" }} world
Output
hello world
goodbye world
Example Default Numeric Query ParameterProvide a default value for a numeric query parameter
Liquid
{% var page = request.query_params['page'] | to_int | default: 1 %}
Example How to use the default filterUse the default filter to output a fallback when a value is null, empty, false, or invalid. Shown for strings, numbers (e.g. query parameters), and optional display.

String default

Liquid
{%- var greeting = "" -%}
{{- greeting | default: "hello" }} world
{%- set greeting = "goodbye" -%}
{{- greeting | default: "hello" }} world
Output
hello world
goodbye world

Output a default value when the input string is null or empty.

Numeric default (e.g. query parameter)

Liquid
{% var page = request.query_params['page'] | to_int | default: 1 %}

Provide a default value for a numeric query parameter when missing or invalid. The to_int filter is used to convert the query parameter to a number if it is not already a number.

Default for an optional custom field

Liquid
{{ item['headline'].value | default: "Untitled" | escape }}

Take .value so default sees a string (or an invalid/empty field), then escape for HTML. Do not pipe a field’s default output through default as if it were a plain string, and do not use default on entity.title.

Default with filters in a chain

Liquid
{{ classname_prefix | strip | default: 'fallback-' | append: entity.name.value | classname }}

Combine default with other filters; filters are applied in the order they are listed.

Example Convert query parameters to booleans and numbersShows how to convert query string parameters into strongly-typed values using to_boolean, to_int, and to_number.

Convert a query parameter to boolean

Liquid
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}

Sets the show_buttons variable to true unless the show_btns query parameter is present and set to false.

Convert a query parameter to integer

Liquid
{% var limit = request.query_params['limit'] | to_int | default: 20 %}

Sets the limit variable to 20 unless the limit query parameter is present and set to a valid integer.

Convert a query parameter to number

Liquid
{% var average = request.query_params['average'] | to_number | default: 2.5 %}

Sets the average variable to 2.5 unless the average query parameter is present and set to a valid number.

Format filter

Returns the input formatted as a string using the provided format string. There are three types of objects that can be formatted, and each type uses its own format strings. If the input object is a string that can be converted to a date, it will be converted to a date before formatting. Otherwise if the input object is a string that can be converted to a number, it will be converted to a number before formatting:
Numbers - Require a valid standard or custom .NET numeric format string. This is identical to using the format_number filter on a number.
Dates - Require a valid standard or custom .NET date format.
Time Diffs - Require a valid standard or custom .NET TimeSpan format.

format: string format
Example Json_encode various object typesUse the json_encode filter to format the input for output as json properties. While most usefulf or strings, this can also be used with other object types.
Liquid
{
"null": {{ null | for_json }},
"date": {{ request.date | json_encode }},
"true": {{ true | json_encode }},
"3": {{ 3 | json_encode }},
"-4.2": {{ -4.2 | json_encode }},
"string": {{ 'Liquid is "cool"' | json_encode -}}
}
Output
{
"null": null,
"date": "2009-06-15T13:45:30.0000000Z",
"true": true,
"3": 3,
"-4.2": -4.2,
"string": "Liquid is \"cool\""
}
Example How to use the format filter to format time diffsFormat time diffs using standard or custom .NET TimeSpan formats.
Liquid
{% var startdate = '2009-09-09 14:00:00Z' | date | to_timezone: 'America/New_York' %}
{% var enddate = '2009-09-10 17:32:00Z' | date | to_timezone: 'America/New_York' %}
{% var diff = startdate | time_diff: enddate %}

Format a time diff as a constant time span string

Liquid
{{ diff | format: "c" }}
Output
-1.03:32:00

Formats a time diff as a constant time span string (c = constant time span).

Format a time diff as a compact time span string

Liquid
{{ diff | format: "g" }}
Output
-1:3:32:00

Format a time diff using a custom .NET TimeSpan format string

Liquid
{% if diff.total_seconds < 0 %}-{% endif %}{{ diff | format: "d' days, 'h' hours, and 'm' minutes'" }}
Output
-1 days, 3 hours, and 32 minutes

Formats a time diff using a custom .NET TimeSpan format string. Since custom .NET TimeSpan format strings do not have a way to output the minus sign for negative time spans, you have to check for negative time spans separately.

Example How to use the format filter to format numbersFormat numbers using standard or custom .NET numeric format strings.

Format a number with 2 decimal places and group separators

Liquid
{{ -1234.5678 | format_number: 'N2' }}
Output
-1,234.57

Uses .NET-style numeric format strings (N2 = number with 2 decimal places and group separator).

Format a number with 4 decimal places and no group separators

Liquid
{{55555.9 | format_number: "F4"}}
Output
55555.9000

Uses .NET-style numeric format strings (F4 = number with 4 decimal places).

Format a number with no decimal places

Liquid
{{5.9 | format_number: "F0"}}
Output
6

Uses .NET-style numeric format strings (F0 = number with no decimal places). The number is rounded as necessary to fit the format string. If you want to round down to the nearest integer, you can use the floor filter instead of the format_number filter.

Format a percent

Liquid
{{ 0.333333 | format: 'P1' }}
Output
33.3%

Uses .NET-style percentage format strings (P1 = percentage with 1 decimal place).

Format an integer with leading zeros

Liquid
{{ 12345 | format: 'D8' }}
Output
00012345

Uses .NET-style decimal format strings (D8 = decimal with 8 digits). The decimal format string is only valid for integers.

Format a number with a custom numeric format string

Liquid
{{ 1234567890 | format: '(###) ###-####' }}
{{ 42 | format: 'My Number = #' }}
Output
(123) 456-7890
My Number = 42

Outputs two numbers formatted with different custom .NET-style format strings.

Example How to use the format filter to format datesFormat dates using standard or custom .NET date formats.
Liquid
{% var sep9 = '2009-09-09 14:00:00Z' | date | to_timezone: 'America/New_York' %}

Format a date as a short date string

Liquid
{{sep9 | format: "d"}}
Output
9/9/2009

Formats a date as a short date string (d = short date).

Format a date as a short date string with the time

Liquid
{{sep9 | format: "g"}}
Output
9/9/2009 10:00 AM

Formats a date as a short date string with the time (g = short date with time).

Format a date as a long date string

Liquid
{{sep9 | format: "D"}}
Output
Sunday, September 9, 2009

Formats a date as a long date string (D = long date).

Format a date as a long date string with the time

Liquid
{{sep9 | format: "f"}}
Output
Sunday, September 9, 2009 10:00 AM

Formats a date as a long date string with the time (f = long date with time).

Format a date according to the ISO 8601 standard

Liquid
{{sep9 | format: "o"}}
Output
9/9/2009 2:00:00 PM

Formats a date according to the ISO 8601 standard (o = round trip format with offset).

Format a date as a sortable date string

Liquid
{{sep9 | format: "u"}}
Output
9/9/2009 2:00:00 PM

Formats a date as a sortable date string (u = universal sortable date in UTC timezone).

Format a date to a long date string using a custom date format string

Liquid
{{sep9 | format: "MMMM dd, yyyy 'at' HH:mm:ss"}}
Output
September 09, 2009 at 10:00:00

Formats a date using a custom .NET date format string.

Format a date to a short date string using a custom date format string

Liquid
{{sep9 | format: "hh:mm tt 'on' MM-dd-yy"}}
Output
10:00 AM on 09-09-09

Formats a date using a custom .NET date format string.

You can also use the date filter to format a date

Liquid
{{sep9 | date: "hh:mm tt 'on' MM-dd-yy"}}
Output
10:00 AM on 09-09-09

The date and format filters use the same format strings for dates, but the date filter can only be used to format dates while the format filter can also be used to format numbers and time diffs.

Example Format dates and numbers (overview)This is a brief example of formatting dates and numbers. There are additional detailed examples of format strings for dates, numbers, and time diffs.

Format a date

Liquid
{% var d = '2009-09-09 14:00:00Z' | date | to_timezone: 'America/New_York' %}
{{ d | format: "d" }}
Output
9/9/2009

Use the format filter with .NET date format strings (e.g. "d" for short date).

Format a number

Liquid
{{ -1234.5678 | format_number: 'N2' }}
Output
-1,234.57

Use the format_number filter with .NET numeric format strings (e.g. N2 for number with 2 decimals and group separator).

Inspect filter

Returns a json-like representation of the current object up to depth layers deep (max 10). Will not load new information from the server unless forceLoad is set to true. Useful during template development and debugging, but do not rely on the result of the inspect tag for your live site.

inspect: number depth, boolean force_load
Example Inspect an unknown fieldUse the inspect filter to learn or troubleshoot the properties of an unknown field or object.

Inspect up to 5 layers deep, but do not load any new data from the server

Liquid
{{entity.what_is_this | inspect: 5}}

Inspect up to 3 layers deep, and load any necessary data from the server

Liquid
{{variable | inspect: 3, true}}

object_type filter

Returns a string identifying the type of the input object. If generic_object_check is true, will return &quot;object&quot; for most objects. If generic_object_check is false or unspecified, will return the value of {{ object.object_type }} for objects with an object_type property.

object_type: boolean generic_object_check

The most common object_type values are: null, object, string, boolean, date, number, list, other specific object types (article, blog_post, etc...)

Example Output the type of an unknown variableUse the object_type filter to get the type of an unknown property or variable.
Liquid
{%- blog_post post = "post" -%}
{{- post | object_type -}}
{{- post | object_type: true }}
Output
blog_post
object

rand filter

Returns a random value. Can behave differently depending on both the type of input and on the arguments supplied. Unless prevent_cache is false, the rand filter will prevent the page from being fast-cached. Note: the rand filter should NOT be considered cryptographically secure - do not use in places where cryptographic security is a requirement (ie: do not use to generate random passwords).

rand: integer length, boolean allow_repeats, boolean prevent_cache

This filter behaves differently depending on the type of input supplied:
Number - If the input is an integer and length is 1, returns a new integer between 1 and the input value. If the input is an integer and length is greater than 1, returns a new list of length numbers between 1 and the input value. If allow_repeats is false, the list returned will be unique. This may result in a list with fewer than length items if the input is less than length.
String - If the input is a string, returns a new random string with length characters, where each character comes directly from the input. If allow_repeats is false, no characters from the input will be used more than once (although any character repeated in the input may be repeated up to the same number of times in the resulting string) - which may result in a string shorter than length if the input string is shorter than length.
List - If the input is a list or list-like object and length is 1, returns a random object from the list. If the input is a list or list-like object and length is greater than 1, returns a new random list of length items from the input list. If allow_repeats is false, no items from the input will be used more than once (although any repeated items in the input may be repeated up to the same number of times in the resulting list) - which may result in a list with fewer than length items if the input list has fewer than length items.

Example Getting random items with the rand filterUse the rand filter to pick one or more random items from a list, get random numbers, or build random strings. Assign results to a variable so the same random choice is used everywhere on the page; use prevent_cache: false when you want the page response to be cacheable.

One random item from an existing list (prevents fast-caching)

Liquid
{%- blog_posts posts = start:1 limit:10 -%}
{%- var randompost = posts | rand -%}
<p>Random pick: {{ randompost.linked_title }}</p>

Get a random item from an existing list. Every time the page is loaded a new random item will be chosen.

One random item from an existing list (allows fast-caching)

Liquid
{%- blog_posts posts = start:1 limit:10 -%}
{%- var randompost = posts | rand:1, false, false -%}
<p>Fast-Cached Random pick: {{ randompost.linked_title }}</p>

Get a random item from an existing list and allow the page to be fast-cached. Subsequent pageloads will have the same random item chosen until the cache expires unless there is something else on the page that prevents fast-caching.

Improved: Fetch only one item from the database

Liquid
{%- blog_posts intermediate = limit:1 sort_by:'random' cache_random:true -%}
{%- var randompost = intermediate | first -%}
<p>Today's pick: {{ randompost.linked_title }}</p>

Functionally equivalent to the previous example, but only one item has to be fetched from the database. With the cache_random:true argument the result may be fast-cached and can be reused for subsequent pageloads and without the cache_random argument the page will not able to be fast-cached.

Multiple random items, no duplicates

Liquid
{%- var numbers = (10..20) -%}
{%- var randomitems = numbers | rand:3, false -%}
{{- randomitems | join:' ' -}}

Get 3 random numbers between 10 and 20 without repeats and outputs them as a space-separated list. Because the third argument is not provided the page will not be fast-cached and every pageload will have a new set of random numbers.

Multiple random numbers with duplicates allowed

Liquid
{%- var rolls = 6 | rand: 4 -%}
<p>Dice rolls: {{ rolls | join: ', ' }}</p>

Get 4 random numbers between 1 and 6 and output them as a comma-separated list. Because the second argument is not provided, duplicates are allowed. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random numbers.

Multiple random numbers with duplicates allowed and fast-cacheable

Liquid
{%- var rolls = 6 | rand: 4, true, false -%}
<p>Dice rolls: {{ rolls | join: ', ' }}</p>

Functionally identical to the previous example except the page may be fast-cached so that future pageloads are significantly faster, but may include the same set of random numbers until the cache expires.

Multiple unique random numbers

Liquid
{%- var picks = 10 | rand: 4, false -%}
<p>Unique picks: {{ picks | join: ' ' }}</p>

Gets 4 unique random numbers between 1 and 10 and outputs them as a space-separated list. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random numbers.

Single random number between 0 and 10

Liquid
{{ 11 | rand: 1 | minus: 1 }}

To get a random number starting with 0 we need to subtract 1 from the result. To allow the page to be fast-cached: Specify either true or false for the second argument (it doesn't matter which for this example) and specify false for the third argument.

Random character from a string (cache in page)

Liquid
{{ "aaabcdeeefghjkmnpqrstuuuvwxyz123456789-_" | rand }}

Gets a single random character from the set of characters in the string. In this example, the set of characters includes lowercase letters, numbers, and the hyphen and underscore characters with some characters excluded and some repeated to increase their probability of being chosen.

Random alphanumeric string

Liquid
{{ "abcdefghijklmnopqrstuvwxyz0123456789" | rand: 10 }}

Pick 10 random characters from the set, with duplicates allowed. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random characters.

Random alphanumeric string without duplciates

Liquid
{{ "abcdefghijklmnopqrstuvwxyz0123456789-----_____" | rand: 10, false }}

Pick 10 random characters from the set, with no duplicates allowed. However, because the set itself contains 5 hyphens and 5 underscores the result may also contain up to 5 hyphens and 5 underscores. Because the third argument is provided as false, the page will not be fast-cached and every pageload will have a new set of random characters.

to_boolean filter

Converts the input to true or false if possible. If not returns null.

to_boolean
Example Convert to booleanConvert a query parameter to a boolean value with a default value of true.
Liquid
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}
Example Convert query parameters to booleans and numbersShows how to convert query string parameters into strongly-typed values using to_boolean, to_int, and to_number.

Convert a query parameter to boolean

Liquid
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}

Sets the show_buttons variable to true unless the show_btns query parameter is present and set to false.

Convert a query parameter to integer

Liquid
{% var limit = request.query_params['limit'] | to_int | default: 20 %}

Sets the limit variable to 20 unless the limit query parameter is present and set to a valid integer.

Convert a query parameter to number

Liquid
{% var average = request.query_params['average'] | to_number | default: 2.5 %}

Sets the average variable to 2.5 unless the average query parameter is present and set to a valid number.

to_int filter

Converts the input to an integer.

to_int

If the input is null, returns 0. If the input is non-null and cannot be convert to an integer, returns null.

Example Default Numeric Query ParameterProvide a default value for a numeric query parameter
Liquid
{% var page = request.query_params['page'] | to_int | default: 1 %}
Example Convert to integerConvert a query parameter to an integer value with a default value.
Liquid
{% var limit = request.query_params['limit'] | default: 20 | to_int %}
Example Convert query parameters to booleans and numbersShows how to convert query string parameters into strongly-typed values using to_boolean, to_int, and to_number.

Convert a query parameter to boolean

Liquid
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}

Sets the show_buttons variable to true unless the show_btns query parameter is present and set to false.

Convert a query parameter to integer

Liquid
{% var limit = request.query_params['limit'] | to_int | default: 20 %}

Sets the limit variable to 20 unless the limit query parameter is present and set to a valid integer.

Convert a query parameter to number

Liquid
{% var average = request.query_params['average'] | to_number | default: 2.5 %}

Sets the average variable to 2.5 unless the average query parameter is present and set to a valid number.

DEPRECATED for_json filter

This filter has been deprecated. You should use the json_encode filter instead. Encode a string to be used output as JSON. Unlike json_encode, if the string is null this will return an empty string instead.

for_json
Example Json_encode various object typesUse the json_encode filter to format the input for output as json properties. While most usefulf or strings, this can also be used with other object types.
Liquid
{
"null": {{ null | for_json }},
"date": {{ request.date | json_encode }},
"true": {{ true | json_encode }},
"3": {{ 3 | json_encode }},
"-4.2": {{ -4.2 | json_encode }},
"string": {{ 'Liquid is "cool"' | json_encode -}}
}
Output
{
"null": null,
"date": "2009-06-15T13:45:30.0000000Z",
"true": true,
"3": 3,
"-4.2": -4.2,
"string": "Liquid is \"cool\""
}
Example Use for_json when serializing to JSON (deprecation note)Use the for_json filter on variables that both do and do not have values
Liquid
{"something": {{'with "value"' | for_json}} }
{"something": {{null | for_json}} }
Output
{"something": "with \"value\""}
{"something": }

The for_json filter works for input with valid values

Liquid
{"something": {{'with "value"' | for_json}} }
Output
{"something": "with \"value\""}

The for_json filter does not produce output if the input is null

Liquid
{"something": {{null | for_json}} }
Output
{"something": }

You can get away with the for_json filter by manually handling null/invalid values

Liquid
{"something": {% if variable is_valid %}{{variable | for_json}}{% else %}null{% endif %} }

The entire for_json filter is deprecated because the json_encode filter handles this and other uses cases more robustly

Liquid
{"something": {{variable | json_encode }} }

to_number filter

Converts the input to a number.

to_number

If the input is null, returns 0. If the input is non-null and cannot be convert to an integer, returns null.

Example Convert to numberConvert a query parameter to a number with a default value.
Liquid
{% var average = request.query_params['average'] | default: 2.5 | to_number %}
Example Convert query parameters to booleans and numbersShows how to convert query string parameters into strongly-typed values using to_boolean, to_int, and to_number.

Convert a query parameter to boolean

Liquid
{% var show_buttons = request.query_params['show_btns'] | to_boolean | default:true %}

Sets the show_buttons variable to true unless the show_btns query parameter is present and set to false.

Convert a query parameter to integer

Liquid
{% var limit = request.query_params['limit'] | to_int | default: 20 %}

Sets the limit variable to 20 unless the limit query parameter is present and set to a valid integer.

Convert a query parameter to number

Liquid
{% var average = request.query_params['average'] | to_number | default: 2.5 %}

Sets the average variable to 2.5 unless the average query parameter is present and set to a valid number.

to_timezone filter

Converts a date to the specified timezone.

to_timezone: string timezone
Example How to use the format filter to format datesFormat dates using standard or custom .NET date formats.
Liquid
{% var sep9 = '2009-09-09 14:00:00Z' | date | to_timezone: 'America/New_York' %}

Format a date as a short date string

Liquid
{{sep9 | format: "d"}}
Output
9/9/2009

Formats a date as a short date string (d = short date).

Format a date as a short date string with the time

Liquid
{{sep9 | format: "g"}}
Output
9/9/2009 10:00 AM

Formats a date as a short date string with the time (g = short date with time).

Format a date as a long date string

Liquid
{{sep9 | format: "D"}}
Output
Sunday, September 9, 2009

Formats a date as a long date string (D = long date).

Format a date as a long date string with the time

Liquid
{{sep9 | format: "f"}}
Output
Sunday, September 9, 2009 10:00 AM

Formats a date as a long date string with the time (f = long date with time).

Format a date according to the ISO 8601 standard

Liquid
{{sep9 | format: "o"}}
Output
9/9/2009 2:00:00 PM

Formats a date according to the ISO 8601 standard (o = round trip format with offset).

Format a date as a sortable date string

Liquid
{{sep9 | format: "u"}}
Output
9/9/2009 2:00:00 PM

Formats a date as a sortable date string (u = universal sortable date in UTC timezone).

Format a date to a long date string using a custom date format string

Liquid
{{sep9 | format: "MMMM dd, yyyy 'at' HH:mm:ss"}}
Output
September 09, 2009 at 10:00:00

Formats a date using a custom .NET date format string.

Format a date to a short date string using a custom date format string

Liquid
{{sep9 | format: "hh:mm tt 'on' MM-dd-yy"}}
Output
10:00 AM on 09-09-09

Formats a date using a custom .NET date format string.

You can also use the date filter to format a date

Liquid
{{sep9 | date: "hh:mm tt 'on' MM-dd-yy"}}
Output
10:00 AM on 09-09-09

The date and format filters use the same format strings for dates, but the date filter can only be used to format dates while the format filter can also be used to format numbers and time diffs.

Example Convert or display dates in a timezone (to_timezone filter)Demonstrates multiple ways to use the to_timezone filter.

Display the article post date in multiple timezones

Liquid
{{ article.post_date | to_timezone: session.user_timezone }} local time
{{- article.post_date | to_timezone: 'UTC' }} UTC
{{- article.post_date | to_timezone: 'Europe/Rome' }} CET
Output
2026-02-16 6:00:00 local time
2026-02-16 10:00:00 UTC
2026-02-16 11:00:00 CET

Convert a date to a different timezone and display it with formatting

Liquid
{%- if entity.alt_timezone.is_valid -%}
	{{- entity.alt_date | to_timezone: entity.alt_timezone | date: 'f' }} {{ entity.alt_timezone -}}
{%- endif -%}
Output
2026-02-16 12:00:00