NAME

Template::Liquid::Filters - Default Filters Based on Liquid's Standard Set

Synopsis

Filters are simple methods that modify the output of numbers, strings, variables and objects. They are placed within an output tag {{ }} and are denoted by a pipe character |.

# product.title = "Awesome Shoes"
{{ product.title | upcase }}
# Output: AWESOME SHOES

In the example above, product is the object, title is its attribute, and upcase is the filter being applied.

Some filters require a parameter to be passed.

{{ product.title | remove: "Awesome" }}
# Output: Shoes

Multiple filters can be used on one output. They are applied from left to right.

{{ product.title | upcase | remove: "AWESOME"  }}
# SHOES

Standard Filters

These are the current default filters. They have been written to behave exactly like their Ruby Liquid counterparts accept where Perl makes improvement irresistible.

abs

Returns the absolute value of a number.

{{  4 | abs }} => 4
{{ -4 | abs }} => 4

append

Append a string.

{{ 'foo' | append:'bar' }} => 'foobar'

at_least

Limits a number to a minimum value.

{{ 4 | at_least: 5 }} => 5
{{ 4 | at_least: 3 }} => 4

at_most

Limits a number to a maximum value.

{{ 4 | at_most: 5 }} => 4
{{ 4 | at_most: 3 }} => 3

capitalize

Capitalize words in the input sentence. This filter first applies Perl's lc function and then the ucfirst function.

{{ 'this is ONLY a test.' | capitalize }} => This is only a test.

ceil

Rounds an integer up to the nearest integer.

{{ 4.6 | ceil }} => 5
{{ 4.3 | ceil }} => 5

compact

Removes any undefined values from an array.

For this example, assume site.pages is an array of content pages for a website, and some of these pages have an attribute called category that specifies their content category. If we map those categories to an array, some of the array items might be undefined if any pages do not have a category attribute.

{% assign all_categories = site.pages | map: "category" %}

{% for item in all_categories %}
- {{ item }}
{% endfor %}

The output of this template would look like this:

    - business
    - celebrities
	-
    - lifestyle
    - sports
	-
    - technology

By using compact when we create our site_categories array, we can remove all the nil values in the array.

{% assign all_categories = site.pages | map: "category" | compact %}

{% for item in all_categories %}
- {{ item }}
{% endfor %}

The output of this template would look like this:

- business
- celebrities
- lifestyle
- sports
- technology

concat

Concatenates (joins together) multiple arrays. The resulting array contains all the items from the input arrays.

{% assign fruits = "apples, oranges, peaches" | split: ", " %}
{% assign vegetables = "carrots, turnips, potatoes" | split: ", " %}

{% assign everything = fruits | concat: vegetables %}

{% for item in everything %}
- {{ item }}
{% endfor %}

...becomes...

- apples
- oranges
- peaches
- carrots
- turnips
- potatoes

You can string together two or more array elements with the concat filter:

{% assign furniture = "chairs, tables, shelves" | split: ", " %}
{% assign vegetables = "carrots, turnips, potatoes" | split: ", " %}
{% assign fruits = "apples, oranges, peaches" | split: ", " %}

{% assign everything = fruits | concat: vegetables | concat: furniture %}

{% for item in everything %}
- {{ item }}
{% endfor %}

...becomes...

- apples
- oranges
- peaches
- carrots
- turnips
- potatoes
- chairs
- tables
- shelves

date

Converts a timestamp into another date format. The format for this syntax is the same as strftime.

{{ article.published_at | date: "%a, %b %d, %y" }} => Fri, Jul 17, 15

{{ article.published_at | date: "%Y" }} => 2015

date works on strings if they contain well-formatted dates:

{{ "March 14, 2016" | date: "%b %d, %y" }} => Mar 14, 16

Natural language dates are parsed by DateTime::Format::Natural.

To get the current time, pass the special word <"now"> (or "today") to date:

  This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
	=> This page was last updated at 2019-09-19 17:48.

Note that the value will be the current time of when the page was last generated from the template, not when the page is presented to a user if caching or static site generation is involved.

default

Allows you to specify a fallback in case a value doesn't exist. default will show its value if the left side is nil, false, or empty.

In this example, product_price is not defined, so the default value is used.

{{ product_price | default: 2.99 }} => 2.99

In this example, product_price is defined, so the default value is not used.

{% assign product_price = 4.99 %}
{{ product_price | default: 2.99 }} => 4.99

In this example, product_price is empty, so the default value is used.

{% assign product_price = "" %}
{{ product_price | default: 2.99 }} => 2.99

divided_by

Divides a number by another number.

The result is rounded down to the nearest integer (that is, the floor) if the divisor is an integer.

{{ 16 | divided_by: 4 }} => 4


{{ 5 | divided_by: 3 }} = 1

Controlling rounding

divided_by produces a result of the same type as the divisor -- that is, if you divide by an integer, the result will be an integer. If you divide by a float (a number with a decimal in it), the result will be a float.

For example, here the divisor is an integer:

{{ 20 | divided_by: 7 }} => 2

Here it is a float:

{{ 20 | divided_by: 7.0 }} => 2.85714285714286

Note that floats will not match thanks to how perl and ruby handle floating point numbers.

downcase

Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.

{{ "Parker Moore" | downcase }} => parker moore

{{ "apple" | downcase }} => apple

escape

Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn't change strings that don't have anything to escape.

{{ "Have you read 'James & the Giant Peach'?" | escape }}
  => Have you read &#39;James &amp; the Giant Peach&#39;?

{{ "Tetsuro Takara" | escape }} => Tetsuro Takara

escape_once

Escapes a string without changing existing escaped entities. It doesn't change strings that don't have anything to escape.

{{ "1 < 2 & 3" | escape_once }} => 1 &lt; 2 &amp; 3

{{ "1 &lt; 2 &amp; 3" | escape_once }} => 1 &lt; 2 &amp; 3

first

Returns the first item of an array.

{{ "Ground control to Major Tom." | split: " " | first }} => Ground


{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}

{{ my_array.first }}
  => zebra

You can use first with dot notation when you need to use the filter inside a tag:

{% if my_array.first == "zebra" %}
  Here comes a zebra!
{% endif %}
  => Here comes a zebra!

floor

Rounds an integer down to the nearest integer.

{{ 1.2 | floor }} => 1
{{ 2.0 | floor }} => 2
{{ 183.357 | floor }} => 183

Here the input value is a string:

{{ "3.5" | floor }} => 3

join

Combines the items in an array into a single string using the argument as a separator.

{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{{ beatles | join: " and " }} => John and Paul and George and Ringo

last

Returns the last item of an array.

{{ "Ground control to Major Tom." | split: " " | last }} => Tom.

{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{{ my_array.last }} => tiger

You can use last with dot notation when you need to use the filter inside a tag:

{% assign my_array = "zebra, octopus, giraffe, tiger" | split: ", " %}
{% if my_array.last == "tiger" %}
   There goes a tiger!
 {% endif %}

lstrip

Removes all whitespace (tabs, spaces, and newlines) from the left side of a string. It does not affect spaces between words.

{{ "          So much room for activities!          " | lstrip }}
  => So much room for activities!

map

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

In this example, assume the object site.pages contains all the metadata for a website. Using assign with the map filter creates a variable that contains only the values of the category properties of everything in the site.pages object.

{% assign all_categories = site.pages | map: "category" %}

{% for item in all_categories %}
- {{ item }}
{% endfor %}

The output of this template would look like this:

- business
- celebrities
- lifestyle
- sports
- technology

minus

Subtracts a number from another number.

{{ 4 | minus: 2 }} => 2

{{ 16 | minus: 4 }} => 12

{{ 183.357 | minus: 12 }} => 171.357

modulo

Returns the remainder of a division operation.

{{ 3 | modulo: 2 }} => 1

{{ 24 | modulo: 7 }} => 3

{{ 183.357 | modulo: 12 }} => 3.357

newline_to_br

Replaces each newline (\n) with html break (<br />\n).

{% capture string_with_newlines %}
Hello
there
{% endcapture %}

{{ string_with_newlines | newline_to_br }}

...becomes...

<br />
Hello<br />
there<br />

plus

Adds a number to another number.

{{ 154    | plus:1183 }}  => 1337
{{ 4 | plus: 2 }} => 6
{{ 16 | plus: 4 }} => 20
{{ 183.357 | plus: 12 }} => 195.357

{{ 'What' | plus:'Uhu' }} => WhatUhu

MATHFAIL!

Please note that integer behavior differs with Perl vs. Ruby so...

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

...becomes 11 in Ruby but 2 in Perl.

prepend

Adds the specified string to the beginning of another string.

{{ 'bar' | prepend:'foo' }} => 'foobar'

{{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}
  => Some fruit: apples, oranges, and bananas

{% assign url = "example.com" %}
{{ "/index.html" | prepend: url }} => example.com/index.html

remove

Removes every occurrence of the specified substring from a string.

{{ 'foobarfoobar' | remove:'foo' }} => 'barbar'

{{ "I strained to see the train through the rain" | remove: "rain" }}
  => I sted to see the t through the

remove_first

Remove the first occurrence of a string.

{{ 'barbar' | remove_first:'bar' }} => bar

{{ "I strained to see the train through the rain" | remove_first: "rain" }}
  => I sted to see the train through the rain

replace

Replaces every occurrence of the first argument in a string with the second argument.

The replacement value is optional and defaults to an empty string ('').

{{ 'foofoo'                 | replace:'foo','bar' }} => barbar
{% assign this = 'that' %}
{{ 'Replace that with this' | replace:this,'this' }} => Replace this with this
{{ 'I have a listhp.'       | replace:'th' }}        => I have a lisp.
{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}
  => Take your protein pills and put your helmet on

replace_first

Replaces only the first occurrence of the first argument in a string with the second argument.

The replacement value is optional and defaults to an empty string ('').

{{ 'barbar' | replace_first:'bar','foo' }} => 'foobar'

{{ "Take my protein pills and put my helmet on" | replace_first: "my", "your" }}
  => Take your protein pills and put my helmet on

reverse

Reverses the order of the items in an array. reverse cannot reverse a string.

{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | reverse | join: ", " }} => plums, peaches, oranges, apples

Although reverse cannot be used directly on a string, you can split a string into an array, reverse the array, and rejoin it by chaining together filters:

{{ "Ground control to Major Tom." | split: "" | reverse | join: "" }}
  => .moT rojaM ot lortnoc dnuorG

round

Rounds a number to the nearest integer or, if a number is passed as an argument, to that number of decimal places.

    {{ 4.6 | round }}        => 5
    {{ 4.3 | round }}        => 4
	{{ 1.2 | round }}        => 1
    {{ 2.7 | round }}        => 3
    {{ 4.5612 | round: 2 }}  => 4.56
	{{ 183.357 | round: 2 }} => 183.36

rstrip

Removes all whitespace (tabs, spaces, and newlines) from the right side of a string. It does not affect spaces between words.

{{ "          So much room for activities!          " | rstrip }}
  =>          So much room for activities!

size

Returns the number of characters in a string, the number of items in an array, or the number of keys in a hash reference. Undefined values return 0.

# Where array is [1..6] and hash is { child => 'blarg'}
{{ array     | size }} => 6
{{ 'Testing' | size }} => 7
{{ hash      | size }} => 1
{{ undefined | size }} => 0
{{ "Ground control to Major Tom." | size }} => 28

{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.size }} => 4

You can use size with dot notation when you need to use the filter inside a tag:

{% if site.pages.size > 10 %}
  This is a big website!
{% endif %}

slice

Returns a substring of 1 character beginning at the index specified by the first argument. An optional second argument specifies the length of the substring to be returned.

String indices are numbered starting from 0.

{{ "Liquid" | slice: 0 }} => L
{{ "Liquid" | slice: 2 }} => q
{{ "Liquid" | slice: 2, 5 }} => quid

If the first argument is a negative number, the indices are counted from the end of the string:

{{ "Liquid" | slice: -3, 2 }} => ui

sort

Sorts items in an array in case-sensitive order.

{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
{{ my_array | sort | join: ", " }} => Sally Snake, giraffe, octopus, zebra

An optional argument specifies which property of the array's items to use for sorting.

{% assign products_by_price = collection.products | sort: "price" %}
{% for product in products_by_price %}
  <h4>{{ product.title }}</h4>
{% endfor %}

sort_natural

Sorts items in an array in case-sensitive order.

{% assign my_array = "zebra, octopus, giraffe, Sally Snake" | split: ", " %}
{{ my_array | sort_natural | join: ", " }}  => giraffe, octopus, Sally Snake, zebra

An optional argument specifies which property of the array's items to use for sorting.

{% assign products_by_company = collection.products | sort_natural: "company" %}
{% for product in products_by_company %}
  <h4>{{ product.title }}</h4>
{% endfor %}

split

Divides a string into an array using the argument as a separator. split is commonly used to convert comma-separated items from a string to an array.

{% assign beatles = "John, Paul, George, Ringo" | split: ", " %}
{% for member in beatles %}
  {{ member }}
{% endfor %}

...becomes...

John
Paul
George
Ringo

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.

|{{ "          So much room for activities!          " | strip }}|
  => |So much room for activities!|

strip_html

Removes any HTML tags from a string.

{{ '<div>Hello, <em id="whom">world!</em></div>' | strip_html }}  => Hello, world!
'{{ '<IMG SRC = "foo.gif" ALT = "A > B">'        | strip_html }}' => ' B">'
'{{ '<!-- <A comment> -->'                       | strip_html }}' => ' -->'
{{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }} => Have you read Ulysses?

Note that this filter uses s[<.*?][]g> in emulation of the Ruby Liquid library's strip_html function. ...so don't email me if you (correctly) think this is a brain dead way of stripping html.

strip_newlines

Removes any newline characters (line breaks) from a string.

{% capture string_with_newlines %} Hello there {% endcapture %}

{{ string_with_newlines | strip_newlines }} => Hellothere

times

Simple multiplication or string repetition.

{{ 'foo' | times: 4 }} => foofoofoofoo
{{ 5 | times: 4 }} => 20
{{ 3 | times: 2 }} => 6
{{ 24 | times: 7 }} => 168
{{ 183.357 | times: 12 }} => 2200.284

truncate

Shortens a string down to the number of characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (...) is appended to the string and is included in the character count.

{{ "Ground control to Major Tom." | truncate: 20 }} => Ground control to...
{{ 'Running the halls!!!' | truncate:19 }}          => Running the hall..
{% assign blarg = 'STOP!' %}
{{ 'Any Colour You Like' | truncate:10,blarg }}     => Any CSTOP!
{{ 'Why are you running away?' | truncate:4,'?' }}  => Why?
{{ 'Ha' | truncate:4 }}                             => Ha
{{ 'Ha' | truncate:1,'Laugh' }}                     => Laugh
{{ 'Ha' | truncate:1,'...' }}                       => ...

...and...

{{ 'This is a long line of text to test the default values for truncate' | truncate }}

...becomes...

This is a long line of text to test the default...

Custom ellipsis

truncate takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (...), but you can specify a different sequence.

The length of the second argument counts against the number of characters specified by the first argument. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first argument of truncate, since the ellipsis counts as 3 characters.

{{ "Ground control to Major Tom." | truncate: 25, ", and so on" }}
  => Ground control, and so on

No ellipsis

You can truncate to the exact number of characters specified by the first argument and avoid showing trailing characters by passing a blank string as the second argument:

{{ "Ground control to Major Tom." | truncate: 20, "" }}
  => Ground control to Ma

truncatewords

Shortens a string down to the number of words passed as an argument. If the specified number of words is less than the number of words in the string, an ellipsis (...) is appended to the string.

{{ "Ground control to Major Tom." | truncatewords: 3 }} => Ground control to...

Custom ellipsis

truncatewords takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (...), but you can specify a different sequence.

{{ "Ground control to Major Tom." | truncatewords: 3, "--" }} => Ground control to--

No ellipsis

You can avoid showing trailing characters by passing a blank string as the second argument:

{{ "Ground control to Major Tom." | truncatewords: 3, "" }} => Ground control to

uniq

Removes any duplicate elements in an array.

{% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}
{{ my_array | uniq | join: ", " }} => ants, bugs, bees

upcase

Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.

{{ "Parker Moore" | upcase }} => PARKER MOORE

{{ "APPLE" | upcase }} => APPLE

url_decode

Decodes a string that has been encoded as a URL or by url_encode.

{{ "%27Stop%21%27+said+Fred" | url_decode }} => 'Stop!' said Fred

url_encode

Converts any URL-unsafe characters in a string into percent-encoded characters.

{{ "john@liquid.com" | url_encode }} => john%40liquid.com

{{ "Tetsuro Takara" | url_encode }} => Tetsuro+Takara

{{ "'Stop!'" said Fred" | url_encode }} => %27Stop%21%27+said+Fred

where

Creates an array including only the objects with a given property value, or any truthy value by default.

In this example, assume you have a list of products and you want to show your kitchen products separately. Using where, you can create an array containing only the products that have a "type" of "kitchen".

All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}

{% assign kitchen_products = products | where: "type", "kitchen" %}

Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %}

...rendered with this data...

products => [
      { title => 'Vacuum',       type => 'carpet', },
      { title => 'Spatula',      type => 'kitchen' },
      { title => 'Television',   type => 'den' },
      { title => 'Garlic press', type => 'kitchen' },
  ]

...becomes...

All products:
- Vacuum
- Spatula
- Television
- Garlic press

Kitchen products:
- Spatula
- Garlic press

Say instead you have a list of products and you only want to show those that are available to buy. You can where with a property name but no target value to include all products with a truthy "available" value.

All products:
{% for product in products %}
- {{ product.title }}
{% endfor %}

{% assign available_products = products | where: "available" %}

Available products:
{% for product in available_products %}
- {{ product.title }}
{% endfor %}

...rendered with this data...

products => [
      { title => 'Coffee mug',               available => 1},
      { title => 'Limited edition sneakers', available => 0},
      { title => 'Boring sneakers',          available => 1}
  ]

...becomes...

All products:
- Coffee mug
- Limited edition sneakers
- Boring sneakers

Available products:
- Coffee mug
- Boring sneakers

The where filter can also be used to find a single object in an array when combined with the first filter. For example, say you want to show off the shirt in your new fall collection.

{% assign new_shirt = products | where: "type", "shirt" | first %}

Featured product: {{ new_shirt.title }}

...rendered with the following data...

products => [
          { title => 'Limited edition sneakers',    type => 'shoes' },
          { title => 'Hawaiian print sweater vest', type => 'shirt' },
          { title => 'Tuxedo print tshirt',         type => 'shirt' },
          { title => 'Jorts',                       type => 'shorts' }
      ]

...becomes...

Featured product: Hawaiian print sweater vest

money

Formats floats and integers as if they were money.

{{  4.6    | money }} => $4.60
{{ -4.3    | money }} => -$4.30
{{  4.5612 | money }} => $4.56

You may pass a currency symbol to override the default dollar sign ($).

{{  4.6    | money:'€' }} => €4.60

stock_price

Formats floats and integers as if they were stock prices.

{{ 4.6    | stock_price }} => $4.60
{{ 0.30   | stock_price }} => $0.3000
{{ 4.5612 | stock_price }} => $4.56

You may pass a currency symbol to override the default dollar sign ($).

{{  4.6    | stock_price:'€' }} => €4.60

Author

Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/

CPAN ID: SANKO

License and Legal

Copyright (C) 2009-2023 by Sanko Robinson <sanko@cpan.org>

This program is free software; you can redistribute it and/or modify it under the terms of The Artistic License 2.0. See the LICENSE file included with this distribution or notes on the Artistic License 2.0 for clarification.

When separated from the distribution, all original POD documentation is covered by the Creative Commons Attribution-Share Alike 3.0 License. See the clarification of the CCA-SA3.0.