Filters (Liquid)

Date filter

date

Formats a date string using specified formats

Date filter reference

Example

Input

{% raw %}{{ '2024-09-05T13:59:07Z' | date:'%F' }}{% endraw %}

Output

{% let date = '2024-09-05T13:59:07Z' | date:'%F' %}
{{ '2024-09-05T13:59:07Z' | date:'%F' }}

Date filter specifiers

Various ISO 8601 formats

Format specifier Output Description
%Y%m%d 20071119 Calendar date (basic)
%F 2007-11-19 Calendar date (extended)
%Y-%m 2007-11 Calendar date, reduced accuracy, specific month
%Y 2007 Calendar date, reduced accuracy, specific year
%C 20 Calendar date, reduced accuracy, specific century
%Y%j 2007323 Ordinal date (basic)
%Y-%j 2007-323 Ordinal date (extended)
%GW%V%u 2007W471 Week date (basic)
%G-W%V-%u 2007-W47-1 Week date (extended)
%GW%V 2007W47 Week date, reduced accuracy, specific week (basic)
%G-W%V 2007-W47 Week date, reduced accuracy, specific week (extended)
%H%M%S 083748 Local time (basic)
%T 08:37:48 Local time (extended)
%H%M 0837 Local time, reduced accuracy, specific minute (basic)
%H:%M 08:37 Local time, reduced accuracy, specific minute (extended)
%H 08 Local time, reduced accuracy, specific hour
%H%M%S,%L 083748,000 Local time with decimal fraction, comma as decimal sign (basic)
%T,%L 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended)
%H%M%S.%L 083748.000 Local time with decimal fraction, full stop as decimal sign (basic)
%T.%L 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended)
%H%M%S%z 083748-0600 Local time and the difference from UTC (basic)
%T%:z 08:37:48-06:00 Local time and the difference from UTC (extended)
%Y%m%dT%H%M%S%z 20071119T083748-0600 Date and time of day for calendar date (basic)
%FT%T%:z 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
%Y%jT%H%M%S%z 2007323T083748-0600 Date and time of day for ordinal date (basic)
%Y-%jT%T%:z 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended)
%GW%V%uT%H%M%S%z 2007W471T083748-0600 Date and time of day for week date (basic)
%G-W%V-%uT%T%:z 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
%Y%m%dT%H%M 20071119T0837 Calendar date and local time (basic)
%FT%R 2007-11-19T08:37 Calendar date and local time (extended)
%Y%jT%H%MZ 2007323T0837Z Ordinal date and UTC of day (basic)
%Y-%jT%RZ 2007-323T08:37Z Ordinal date and UTC of day (extended)
%GW%V%uT%H%M%z 2007W471T0837-0600 Week date and local time and difference from UTC (basic)
%G-W%V-%uT%R%:z 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)

HTML-tag and URL filters

img_url

Example, Liquid for the src attribute in an img-tag

Input

{% raw %}{{ article | img_url:0,50 }}{% endraw %}

Output

/media/article/17f95478-638c-8c2f-b2eb-501ae41bd00b/Cradle-CMS-code-editor_0_50.png

product_img_url

Example product image

Input

{% raw %}{{ products[0] | product_img_url | img_tag:'alt-title'  }}{% endraw %}

Output

{{  products[1] | product_img_url | img_tag:'alt-title'  | escape }}

Example product image in order context

In an order object each item has an image but to able to reach it we need to specify the product id.

Input (the src attribute on an img element)

{% raw %}src="{{ item.image | product_img_url:item.productId,'thumb' }}"{% endraw %}

asset_img_url

Example asset image

Input

{% raw %}{{ settings.featureSectionImage | asset_img_url:0,50 }}{% endraw %}

Output

/assets/docker-logo-blue_0_50.png

link_tag

Outputs <a href="url"...></a> link tag

Example

Input

{% raw %}{{ vendors[0] | url | link_tag:vendors[0].name }}{% endraw %}

Output

{{ vendors[0] | url | link_tag:vendors[0].name | escape }}

script_tag

Outputs <script type="text/javascript" ...></script> tag for a link.

Example, filename and asset_url filter

Input

{% raw %}{{ 'myscript.js' | asset_url | script_tag }}{% endraw %}

Output

{{ 'myscript.js' | asset_url | script_tag | escape }}

Example, filename with path

Input

{% raw %}{{ '/assets/myscript.js' | script_tag }}{% endraw %}

Output

{{ '/assets/myscript.js' | script_tag | escape }}

Example, full url

Input

{% raw %}{{ 'https://cradlecms.com/assets/myscript.js' | script_tag }}{% endraw %}

Output

{{ 'https://cradlecms.com/assets/myscript.js' | script_tag | escape }}

stylesheet_tag

Outputs <link rel="stylesheet" ...> tag

Example, filename and asset_url filter

Input

{% raw %}{{ 'mystyle.css' | asset_url | script_tag }}{% endraw %}

Output

{{ 'mystyle.css' | asset_url | script_tag | escape }}

Example, filename with path

Input

{% raw %}{{ '/assets/mystyle.css' | script_tag }}{% endraw %}

Output

{{ '/assets/mystyle.css' | script_tag | escape }}

Example, full URL

Input

{% raw %}{{ 'https://cradlecms.com/assets/mystyle.css' | script_tag }}{% endraw %}

Output

{{ 'https://cradlecms.com/assets/mystyle.css' | script_tag | escape }}

url

Prints the URL path for an object

Example

Input

{% raw %}{{ article | url }}{% endraw %}

Output

{{ article | url }}

Special filters

def

Returns a default value if the left side is undefined

Example

Input

{% raw %}{{ something_undefined | def:"The variable was not defined" }}{% endraw %}

Output

{% let undef = something_undefined | def:"The variable was not defined" %}
{{ something_undefined | def:"The variable was not defined" }}

related

Returns related collections, products, pages or articles having shared properties 'tag','vendor','collection','type' when in the object context.

Example

Input

{% raw %}{% assign relatedArticles = article | related:'tag' %}
    {% for article in relatedArticles limit:5 %}

{{ article.meta.h1 }}

{% endfor %}{% endraw %}

Output

{% assign relatedArticles = article | related:'tag' %}
{% for article in relatedArticles limit:5 %}

{{ article.meta.h1 }}

{% endfor %}

Liquid snippet for related products

{% raw %}{% assign relatedProducts = product | related %}
{% for product in relatedProducts limit:5 %}
    {% include 'productCard' %}
{% endfor %}{% endraw %}

render

Serverside renders the content. Normally the content of a page, page.html, is not rendered but addning the filter will render the content and liquid can be used in the page editor.

Example

Input

{% raw %}{{ 'article.html' | render }}{% endraw %}

Output

Liquid tags will be replaced within the content.

url_encode

Url encodes a string

Example

Input

{% raw %}{% '100% of all ÅÄÖ' | url_encode %}}{% endraw %}

Output

{% let pt = '100% of all ÅÄÖ' | url_encode %}
{{ pt }}

Math filters

ceil

Rounds number up to closest integer

Example

Input

{% raw %}{{ 4.6 | ceil }}{% endraw %}

Output

{% let ceil = 4.6 | ceil %}
{{ 4.6 | ceil }}

divided_by

Integer division (in which the fractional part, the remainder, is discarded)

Example

Input

{% raw %}{{ 7 | divided_by:3 }}{% endraw %}

Output

{% let divided = 7 | divided_by:3 %}
{{ 7 | divided_by:3 }}

floor

Rounds number down to closest integer

Example

Input

{% raw %}{{ 4.6 | floor }}{% endraw %}

Output

{% let floored = 4.6 | floor %}
{{ 4.6 | floor }}

minus

Subtraction by an integer

Example

Input

{% raw %}{{ 3 | minus:1 }}{% endraw %}

Output

{{ 3 | minus:1 }}

Example with decimals

Input

{% raw %}{{ 3.6 | minus:1.4 }}{% endraw %}

Output

{{ 3.6 | minus:1.4 }}

modulo

Returns the remainder after division

Example

Input

{% raw %}{{ 7 | modulo:4 }}{% endraw %}

Output

{{ 7 | modulo:4 }}

Example

Input

{% raw %}{{ 37 | modulo:5 }}{% endraw %}

Output

{{ 37 | modulo:5 }}

plus

Addition of integers

Example

Input

{% raw %}{{ '1' | plus:'2'  }}{% endraw %}

Output

{{ '1' | plus:'2'  }}

Example with decimals

Input

{% raw %}{{ '13.5' | plus:'2.9'  }}{% endraw %}

Output

{{ '13.5' | plus:'2.9'  }}

Example with variables

Addition of subtotal and the tax in a checkout form
{% raw %}{% assign x = form.subTotal %}
{% assign y = form.totalVAT %}
{% assign totalProducts = x | plus:y %}{% endraw %}

round

Rounds input to the number of decimals

Example

Input

{% raw %}{{ 3.141562 | round: 2 }}{% endraw %}

Output

{% let pi = 3.141562 | round: 2 %}
{{ 3.141562 | round: 2 }}

times

Multiplication by integer

Example

Input

{% raw %}{{ 2 | times:3 }}{% endraw %}

Output

{% let multiplied = 2 | times:3 %}
{{ 2 | times:3 }}

Example, with decimals

Input

{% raw %}{{ 2.5 | times:3.7 }}{% endraw %}

Output

{% let multiplied = 2.5 | times:3.7 %}
{{ 2.5 | times:3.7 }}

String and Array filters

append

Appends a string

Example

Input

{% raw %}{{ 'test' | append:'-' | append:'me' }}{% endraw %}

Output

{% let append = 'test' | append:'-' | append:'me' %}
{{ 'test' | append:'-' | append:'me' }}

capitalize

Capitalizes the first word

Example

Input

{% raw %}{{ 'hello world' | capitalize }}{% endraw %}

Output

{{ 'hello world' | capitalize }}

downcase

Converts input to lowercase

Example

Input

{% raw %}{{ "WasCamelCased" | downcase }}{% endraw %}

Output

{{ "WasCamelCased" | downcase }}

escape

Escapes special characters like < to become {{ "<" | escape }}. It escapes only five such characters: <, >, &, ' and .

Example

Input

{% raw %}{{ "Click > Here & There" | escape }}{% endraw %}

Output

Note that the browser will not show the HTML Character Entity codes
{{ 'Click > Here & There' | escape }}

first

Returns the first element of an array

Example

We first create an array named fish from a string.

Input

{% raw %}{% assign fish = "Cod,Mackerel,Trout,Salmon" | split: "," %}
{{ fish | first }}{% endraw %}

Output

{% assign fish = "Cod,Mackerel,Trout,Salmon" | split: "," %} {% let firstone = fish | first %}
{{ fish | first }}

last

Returns the last element of an array

Example

Input

{% raw %}{{ fish | last }}{% endraw %}

Output

{% let lastone = fish | last %}
{{ fish | last }}

handleize

Transforms string to a handle format

Example

Input

{% raw %}{{ 'jeans collection' | handleize }}{% endraw %}

Output

{% let handled = 'jeans collection' | handleize %}
{{ 'jeans collection' | handleize }}

insert

insert replaces "%s %1..%n" in source with matching str[arg-n]

Example

Input

{% raw %}{{'insert %s here' | insert:'something'}}{% endraw %}

Output

"insert something here"

Example

Input

{% raw %}{{'hello %1%2' | insert:'world','!'}}{% endraw %}

Output

"hello world!"

join

Joins elements of array with a separator

Example

Input

{{ fish | print_r }}
{% raw %}{{ fish | join:', ' }}{% endraw %}

Output

{% let fishlist = fish | join:', ' %}
{{ fish | join:', ' }}

strip

Strips whitespace from both sides of a string

Example

Input

{% raw %}{{ " hello " | strip }}{% endraw %}

Output

{{ " hello " | strip | print_r }}

lstrip

Strips whitespace from the left side of a string

Example

Input

{% raw %}{{ " hello " | lstrip }}{% endraw %}

Output

{{ " hello " | lstrip | print_r }}

rstrip

Strips whitespace from the right side of a string

Example

Input

{% raw %}{{ " hello " | rstrip }}{% endraw %}

Output

{{ " hello " | rstrip | print_r }}

map

Select/map a given property from an associative array (hash)

Example

Input

{% raw %}{{ blogs | map:'title' | join:', ' }}{% endraw %}

Output

{{ blogs | map:'title' | join:', ' }}

newline_to_br

Example

Input

{% raw %}{{ "I have a multiline string!\nThat I want to output in html\n" | newline_to_br }}{% endraw %}

Output

{{ "I have a multiline string!\nThat I want to output in html\n" | newline_to_br }}

pluralize

Returns the second word if the input is not 1

Example

Input

{% raw %}{{ 2 | pluralize: 'car', 'cars' }}{% endraw %}

Output

{{ 2 | pluralize: 'car', 'cars' }}

prepend

Prepend a string

Example

Input

{% raw %}{{ 'world' | prepend:'hello ' }}{% endraw %}

Output

{% let prepended = 'world' | prepend:'hello ' %}
{{ 'world' | prepend:'hello ' }}

remove_first

Remove the first occurrence

Example

Input

{% raw %}{{ 'Clean tetest!' | remove_first:'te'  }}{% endraw %}

Output

{% let cleaned = 'Clean tetest!' | remove_first:'te' %}
{{ 'Clean tetest!' | remove_first:'te' }}

remove

Remove each occurrence

Example

Input

{% raw %}{{ 'foobarfoo' | remove:'foo' }}{% endraw %}

Output

{% let removed = 'foobarfoo' | remove:'foo' %}
{{ 'foobarfoo' | remove:'foo' }}

replace_first

Replaces the first occurrence

Example

Input

{% raw %}{{ 'barbar' | replace_first:'bar','foo' }}{% endraw %}

Output

{% let replaced = 'barbar' | replace_first:'bar','foo' %}
{{ 'barbar' | replace_first:'bar','foo' }}

replace

Example

Input

{% raw %}{{ 'foofoo' | replace:'foo','bar' }}{% endraw %}

Output

{% let replacedall = 'foofoo' | replace:'foo','bar' %}
{{ 'foofoo' | replace:'foo','bar' }}

reverse

Reverses an array

Example

Input

{% raw %}{% assign abc = "a,b,c" | split: "," %}
{{ abc | reverse }}{% endraw %}

Output

{% assign abc = "a,b,c" | split: "," %} {% let reverses = abc | reverse | join:', ' %}
{{ abc | reverse }}

size

Returns the length of an array or string

Example, string

Input

{% raw %}{{ "what is the size of this" | size }}{% endraw %}

Output

{{ "what is the size of this" | size }}

It is also possible to check for the size in an if statement

{% raw %}{% if article.excerpt.size > 0 %}
    {{ article.excerpt | strip_html }}
{% endif %}{% endraw %}

slice

Slice an array or string using an offset and a length

Example

Input

{% raw %}{{ "hello world" | slice: 1, 3 }}{% endraw %}

Output

{{ "hello world" | slice: 1, 3 }}

sort

Sorts array elements in alphabetical order

Example

Input

{% raw %}{% assign fruit = "Banana,Date,Ananas,Citrus" | split: "," %}
{{ fruit | sort | join:', ' }}{% endraw %}

Output

{% assign fruit = "Banana,Date,Ananas,Citrus" | split: "," %} {% let sorted = fruit | sort | join:', ' %}
{{ fruit | sort | join:', '}}

split

Returns an array by spliting a string on a matching separator

Example

Input

{% raw %}{% let a = "a,b,c,d" | split:"," %}{% endraw %}

Output

{% let a = "a,b,c,d" | split:"," %} {{ a | print_r }}

strip_html

Strip HTML from string

Example

Input

{% raw %}{{ "Testing" | strip_html }}{% endraw %}

Output

{{ "Testing" | strip_html }}

strip_newlines

Strips all newlines \n from strings

Example

Input

{% raw %}{{ "Lorem\n ipsum\n dolor\n sit\n amet" | strip_newlines }}{% endraw %}

Output

{{ "Lorem\n ipsum\n dolor\n sit\n amet" | strip_newlines }}

truncate

Truncates a string to number of characters with optional ellipsis parameter. Note that the ellipsis characters are counted into the amount or characters.

Example, truncates to 16 characters in total with ... in the end.

Input

{% raw %}{{ 'this is to long for me' | truncate: 16,'...' }}{% endraw %}

Output

{{ 'this is to long for me' | truncate: 16,'...' }}

Example, truncates to 4 characters ending with !.

Input

{% raw %}{{ '1234567890' | truncate: 4,'!' }}{% endraw %}

Output

{{ '1234567890' | truncate: 4,'!' }}

Example, with empty ellipsis

Input

{% raw %}{{ '1234567890' | truncate: 4,'' }}{% endraw %}

Output

{{ '1234567890' | truncate: 4,'' }}

truncatewords

Truncates a string to number of words with optional ellipsis parameter

<string> | truncatewords:<int number> [, <string ellipsis, default '...'> ]

Example, one word and default ellipsis

Input

{% raw %}{{ 'hello world' | truncatewords:1 }}{% endraw %}

Output

{{ 'hello world' | truncatewords:1 }}

Example, four words and empty ellipsis

Input

{% raw %}{{ 'Lorem ipsum dolor sit amet.' | truncatewords:4, '' }}{% endraw %}

Output

{{ 'Lorem ipsum dolor sit amet.' | truncatewords:4, ''  }}

uniq

Removes duplicates from an array, optionally using a property parameter, like price, to test with, uniq:'price'

Example

Input

{% raw %}{% let duplicates = "a,a,b,b,e,e,e,c,d" | split:"," %}
{{ duplicates | uniq }}{% endraw %}

Output

{% let duplicates = "a,a,b,b,e,e,e,c,d" | split:"," %} {% let removed = duplicates | uniq | join:',' %}
{{ duplicates | uniq | join:',' }}

upcase

Converts string or array input to uppercase

Example

Input

{% raw %}{{ 'WasCamelCased' | upcase }}{% endraw %}

Output

{% let upcased = 'WasCamelCased' | upcase %}
{{ 'WasCamelCased' | upcase }}

Example

Input

{% raw %}{% assign fruit = "Banana,Date,Ananas,Citrus" | split: "," %}
{{ fruit | upcase }}{% endraw %}

Output

{% assign fruit = "Banana,Date,Ananas,Citrus" | split: "," %}
{{ fruit | upcase }}