Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Latest commit

 

History

History
235 lines (194 loc) · 5.06 KB

filters.md

File metadata and controls

235 lines (194 loc) · 5.06 KB

Filters

Cryptographic

md5

Converts a string to an md5 hash.

{{ 'foo' | md5 }}
{# 'acbd18db4cc2f85cedef654fccc4a4d8' #}

sha1

Converts a string to an sha1 hash.

{{ 'foo' | sha1 }}
{# '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' #}

sha512

Converts a string to an sha512 hash.

{{ 'foo' | sha512 }}
{# '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' #}

Date

time_diff

Use the time_diff filter to render the difference between a date and now.

{{ entry.postDate | time_diff }}
{# '2 days ago' #}

Inflection

pluralize( num=2 )

Converts a string to the plural form.

{{ 'result' | pluralize }}
{# 'results' #}

{{ 'results' | pluralize(1) }}
{# 'result' #}

{{ entries|length }} {{ 'results' | pluralize(entries|length) }}
{# '1 result' or '2 results' #}

singularize

Converts a string to the singular form.

{{ 'cars' | singularize }}
{# 'car' #}

camelize

{{ 'foo_bar' | camelize }}
{# 'FooBar' #}

dasherize

{{ 'foo_bar' | dasherize }}
{# 'foo-bar' #}

humanize

{{ 'employee_salary' | humanize }}
{# 'Employee salary' #}

{{ 'author_id' | humanize }}
{# 'Author' #}

ordinalize

{{ 1 | ordinalize }}
{# '1st' #}

{{ 2 | ordinalize }}
{# '2nd' #}

{{ loop.index | ordinalize }}
{# '3rd' #}

titleize

{{ 'x-men: the last stand' | titleize }}
{# 'X Men: The Last Stand' #}

{{ 'raiders_of_the_lost_ark' | titleize }}
{# 'Raiders Of The Lost Ark' #}

underscore

{{ 'FooBar' | underscore }}
{# 'foo_bar' #}

Internationalization

localizedcurrency

Needs documentation

localizeddate

Use the localizeddate filter to format dates into a localized string representating the date.

{{ entry.postDate | localizeddate('medium', 'none', locale) }}
{# 'Nov 13, 2012' #}
Argument Options Default Description
date_format 'none' Do not include this element 'medium' The date format.
'short' Most abbreviated style, only essential data (12/13/52 or 3:30pm)
'medium' Medium style (Jan 12, 1952)
'long' Long style (January 12, 1952 or 3:30:32pm)
'full' Completely specified style (Tuesday, April 12, 1952 AD or 3:30:42pm PST)
time_format Same formats as listed above. 'medium' The time format.
locale - null The locale used for the format. If NULL is given, Twig will use Locale::getDefault().
timezone - null The date timezone
format - null Optional pattern to use when formatting or parsing. Possible patterns are documented in the ICU user guide.

Internally, this filter uses the PHP IntlDateFormatter::create() function for the date.

localizednumber

Needs documentation

Text

center( width=80 )

Centers the value in a field of a given width.

{{ 'middle' | center(40) }}
{# '                 middle                 ' #}

truncate( length=30, preserve=false, separator='...' )

Use the truncate filter to cut off a string after limit is reached.

{{ "Hello World!" | truncate(5) }}
{# 'Hello...' #}

{{ "Hello World!" | truncate(7, true) }}
{# 'Hello World!' #}

{{ "Hello World!" | truncate(7, false, "??") }}
{# 'Hello W??' #}

wordwrap( length=80, separator="\n" )

Use the wordwrap filter to split your text in lines with equal length.

{{ "Lorem ipsum dolor sit amet, consectetur adipiscing" | wordwrap(10) }}
Lorem ipsu
m dolor si
t amet, co
nsectetur
adipiscing