The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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

Array Filters

Array filters change the output of arrays.

join

Joins elements of the array with a certain character between them.

    # Where array is [1..6]
    {{ array | join }}      => 1 2 3 4 5 6
    {{ array | join:', ' }} => 1, 2, 3, 4, 5, 6

first

Get the first element of the passed in array

    # Where array is [1..6]
    {{ array | first }} => 1

last

Get the last element of the passed in array.

    # Where array is [1..6]
    {{ array | last }} => 6

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

    {% if product.tags.last == "sale"%}
        This product is on sale!
    {% endif %}

Using last on a string returns the last character in the string.

    {{ product.title | last }}

Standard Filters

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

date

Reformat a date...

  • ...by calling the object's strftime method. This is tried first so dates may be defined as a DateTime or DateTimeX::Lite object...

        # On my dev machine where obj is an object built with DateTime->now()
        {{ obj  | date:'%c' }} => Dec 14, 2009 2:05:31 AM
  • ...with the POSIX module's strftime function. This is the last resort and flags may differ by system so... Buyer beware.

        # On my dev machine where date contains the current epoch as an integer
        {{ date | date:'%c' }} => 12/14/2009 2:05:31 AM

The date filter also supports simple replacements of 'now' and 'today' with the current time. For example:

    {{ 'now' | date :'%Y' }}

...would print the current year.

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.

downcase

Convert an input string to lowercase using Perl's lc function.

    {{ 'This is HUGE!' | downcase }} => this is huge!

upcase

Convert a input string to uppercase using Perl's uc function.

split

Split input string into an array of substrings separated by given pattern.

    # Where values is 'foo,bar,baz'
    {{ values | split: ',' | last }} => baz

sort

Sort elements of the array.

    # Where array is defined as [3, 5, 7, 2, 8]
    {{ array | sort }} => 2, 3, 5, 7, 8

size

Return the size of an array, the length of a string, or the number of keys in a hash. 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

strip_html

Strip html from string. Note that this filter uses s[<.*?][]g> in emmulation of the Ruby Liquid library's strip_html function. ...so don't email me if you (correcly) think this is a braindead way of stripping html.

    {{ '<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 }}' => ' -->'

strip_newlines

Strips all newlines (\n) from string using the regular expression s[\n][]g.

newline_to_br

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

replace

Replace all occurrences of a string with another string. 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.

replace_first

Replaces the first occurrence of a string with another string. The replacement value is optional and defaults to an empty string ('').

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

remove

Remove each occurrence of a string.

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

remove_first

Remove the first occurrence of a string.

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

truncate

Truncate a string down to x characters.

    {{ '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...

truncatewords

Truncate a string down to x words.

    {{ 'This is a very quick test of truncating a number of words' | truncatewords:5,'...' }}
    {{ 'This is a very quick test of truncating a number of words where the limit is fifteen' | truncatewords: }}

...becomes...

    This is a very quick...
    This is a very quick test of truncating a number of words where the limit...

prepend

Prepend a string.

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

append

Append a string.

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

minus

Simple subtraction.

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

plus

Simple addition or string contatenation.

    {{ 154    | plus:1183 }}  => 1337
    {{ '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.

times

Simple multiplication or string repetion.

    {{ 'foo' | times:4 }} => foofoofoofoo
    {{ 5     | times:4 }} => 20

divided_by

Simple division.

    {{ 10 | divided_by:2 }} => 5

modulo

Simple modulo operation.

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

round

Rounds the output to the nearest integer or specified number of decimals.

    {{ 4.6 | round }}       => 5
    {{ 4.3 | round }}       => 4
    {{ 4.5612 | round: 2 }} => 4.56

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

abs

Returns the absolute value of a number.

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

ceil

Rounds an integer up to the nearest integer.

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

floor

Rounds an integer down to the nearest integer.

    {{ 4.6 | floor }} => 4
    {{ 4.3 | floor }} => 4

default

Sets a default avlue for any variable with no assigned value. Can be used with strings, arrays, and hashes.

The default value is returne if the cariable resolves to undef or an empty string (''). A string containing whitespace characters will not resolve to the default value.

    {{ customer.name | default: "customer" }} => "customer"

Author

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

CPAN ID: SANKO

License and Legal

Copyright (C) 2009-2016 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.