NAME
Solution::Filter::Standard - Default Filters Based on Liquid's Standard Set
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.
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
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
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.
# Where array is [1..6] and hash is { child => 'blarg'}
{{ array | size }} => 6
{{ 'Testing' | size }} => 7
{{ hash | size }} => 1
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
Author
Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
CPAN ID: SANKO
License and Legal
Copyright (C) 2009-2012 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.