Fork me on GitHub

addslashes

Parameters

None

Description

Adds C style backslashes before quotes. Usually used for CSV output, not for JavaScript.

>> {{ "'abc'" | addslashes }}
=> \'abc\'
>> {{ '"abc"' | addslashes }}
=> \"abc\"

center

Parameters

length
total width of the string you want to center your text in
padding (optional: default = " ")
the character you want to use for the padding

Description

Centers a string in a string of length length. If the string is longer than length no extra padding will be added. padding string specifies which string shall be used to center the string.

>> {{ "hello" | center: 4 }}
=> hello
>> {{ "hello" | center: 20 }}
=>        hello        
>> {{ "hello" | center: 20, "-" }}
=> -------hello--------

cut

Parameters

value
a string to remove from the input

Description

Removes all values from the string

>> {{ "string with spaces" | cut: " " }}
=> stringwithspaces

date

Parameters

format (optional: default = "%F")
The format of the date string

Description

Formats a date value according to the format. The format uses strftime, common to many languages - such as PHP. See the documentation there for specifics on the format.

>> {{ now | date }}
=> 2017-05-01
>> {{ now | date: "%A %B %e %Y %r" }}
=> Monday May  1 2017 12:16:15 AM

default

Parameters

default_value
the value to be used if the input is "falsy"

Description

Uses the default_value passed instead of the value is nil, empty or evaluates to false.

>> {{ nil | default: "Not defined" }}
=> Not defined
>> {{ "" | default: "Not defined" }}
=> Not defined
>> {{ empty_list | default: "Not defined" }}
=> Not defined
>> {{ "abc" | default: "Not defined" }}
=> abc

escape

Parameters

None

Description

Performs HTML escaping on the value. Specifically replaces &, ", <, and > with their HTML special character equivalents.

>> {{ 'foo "bar" <baz>' | escape }}
=> foo &quot;bar&quot; &lt;baz&gt;

first

Parameters

None

Description

Returns the first element of a list

>> {{ alphabet | first }}
=> A

join

Parameters

glue
the string to 'glue' each element together with

Description

Joins together every element of a list with a glue string.

>> {{ alphabet | join: " " }}
=> A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
>> {{ alphabet | join: "." }}
=> A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z

last

Parameters

None

Description

Returns the last element of a list

>> {{ alphabet | last }}
=> Z

length

Parameters

None

Description

Returns the length of a value

>> {{ "abc" | length }}
=> 3
>> {{ alphabet | length }}
=> 26

limit

Parameters

length
the maximum length of the output

Description

Takes the first length items/characters from the collection/string and removes the remainder.

>> {{ alphabet | limit: 5 | join: " " }}
=> A B C D E
>> {{ "Cadenza" | limit: 3 }}
=> Cad

offset

Parameters

count
the number of items to skip before copying

Description

Skips the first count items/characters from a collection/string and returns the remainder.

>> {{ alphabet | offset: 20 | join: " " }}
=> U V W X Y Z
>> {{ "Cadenza" | offset: 5 }}
=> za

pluck

also known as map and collect.

Parameters

attribute
the name of the attribute to pluck

Description

Iterates over every item in the collection and returns the given attribute value in a new collection for every item.

>> {{ people | pluck: "name" | join: " " }}
=> Will Paul Mike
>> {{ people | pluck: "age" | join: " " }}
=> 26 33 42

reverse

Parameters

None

Description

Reverses the input string or collection.

>> {{ "Cadenza" | reverse }}
=> aznedaC
>> {{ alphabet | limit: 5 | reverse | join: " " }}
=> E D C B A

sort

Parameters

attribute (optional: default=None)
the attribute whose value you want to sort by.

Description

Sorts the items in the given collection in ascending order. If an attribute is given then the collection will be sorted in ascending order by the value of that attribute.

If you need to sort in descending order then sort using this method and use a reverse filter after.

>> {{ people | pluck: "age" | sort | reverse }}
=> [42, 33, 26]
>> {{ people | sort: "name" | pluck: "name" }}
=> ["Mike", "Paul", "Will"]
>> {{ people | sort: "age" | pluck: "name" }}
=> ["Will", "Paul", "Mike"]

upper

Parameters

None

Description

Converts the entire string to uppercase.

>> {{ "this was all lower case" | upper }}
=> THIS WAS ALL LOWER CASE

wordwrap

Parameters

number
the maximum width the text will go to in columns before inserting a separator
separator (optional: default = "<br/>")
the string used to separate lines

Description

Takes long text and forces a line break every number of given characters. The type of line break is specified by the optional separator. Gives a nice column effect which is very useful on fixed width fonts.

>> {{ lorem_ipsum | wordwrap: 25 }}
=> Lorem ipsum dolor sit
=> amet, consectetur
=> adipisicing elit