Modifiers/Filters

capitalize

The capitalize filter capitalizes a value. The first character will be uppercase, all others lowercase.

{{ var|capitalize() }}

count

The count filter returns the number of elements in an array.

{{ var|count() }}

cycle

The cycle filter can be used to cycle between an array of values:

{% for i in 0..10 %}
  {{ ['odd', 'even']|cycle(i) }}
{% endfor %}

date

The date filter is able to format a date to a given format:

{{ post.publishedAt|date('m/d/Y') }}
<time datetime="{{ this.publishedAt|date('Y-m-d\\TH:i') }}">
  {{ this.publishedAt|date('F j, Y') }}
</time>

The filter accepts the format used by PHPs date function:

The following characters are recognized in the format parameter string
format
character
DescriptionExample returned values
Day --- ---
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l(lowercase 'L') A full textual representation of the day of the week Sunday throughSaturday
N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters stndrd or th. Works well with j
w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week --- ---
W ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year)
Month --- ---
F A full textual representation of a month, such as January or March January throughDecember
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year --- ---
L Whether it's a leap year 1 if it is a leap year,0 otherwise.
o ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999 or2003
Y A full numeric representation of a year, 4 digits Examples: 1999 or2003
y A two digit representation of a year Examples: 99 or 03
Time --- ---
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds (added in PHP 5.2.2) Example: 654321
Timezone --- ---
e Timezone identifier (added in PHP 5.1.0) Examples: UTC,GMT,Atlantic/Azores
I (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0 otherwise.
O Difference to Greenwich time (GMT) in hours Example: +0200
P Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00
T Timezone abbreviation Examples: EST,MDT ...
Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through50400
Full Date/Time --- ---
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
r » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See also time()

 

default

The default filter returns the passed default value if the value is undefined, otherwise the value of the variable:

{{ var|default('Default Text') }}

urlencode

The urlencode filter URL encodes a given string.

{{ var|urlencode }}

escape/e

The escape filter converts the characters &<>', and " in strings to HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML.

Internally, escape uses the PHP htmlspecialchars function.

{{ var|e }}

even

even returns true if the given number is even:

{{ var|even }}

explode

Splits a string into an array of strings using the specified delimiter. This is typically used when you have a list of items separated by commas or spaces, and want to be able to iterate over them in a loop. An optional second parameter is the maximum number of results to return. If no limit is specified, all of the sub-strings will be returned.

{% set arr as  var|explode(',') %}
{% for item in var|explode(' ', 5) %}

format

The format filter formats a given string by replacing the placeholders:

{# string is a format string like: I like %s and %s. #}
{{ string|format(foo, "bar") }}
{# returns I like foo and bar. (if the foo parameter equals to the foo string) #}

gravatar

The gravatar filter can be applied to an email address to get the url of a Gravatar image. There are three optional parameters: The size in pixels (defaults to 80), the url of a default image to use if the email is not associated with a Gravatar, and the maximum rating you want to display (defaults to 'g').

<img src="{{ comment.email|gravatar(100,hifi.urls.images~'gravatar-default.jpg','r') }}" alt="Gravatar">

imagesize

The imagesize filter is used in templates to resize images and do other processing. The parameters specifying what you want to do to the image should be passed to the filter in the form of a JSON object.

<img src="{{ image_url|imagesize({width:100,height:100,crop:true,bw:true}) }}" alt="Thumbnail" />

Parameters

All parameters are optional.

  • width: The maximum width of the resulting image. If this is not specified the image will only be resized based on the height.
  • height: The maximum height of the resulting image. If this is not specified the image will only be resized based on the width.
  • crop: If this is set to true, the image will be cropped to match the specified width and height exactly. If you leave out the crop parameter, the image will be resized to fit within the specified width and height, but will retain its original aspect ratio. It only makes sense to crop the image if you also specify both the width and the height.
  • bw: If this is set to true the image will be converted to greyscale.
  • colorize: Set this to an rgb hex code to recolor an image. For example, if you wanted a sepia-toned image: {{ image_url|imagesize({colorize:"#704214"}) }}
  • radius: This rounds the corners of the image to the specified radius (in pixels). If you don't specify a background color (see below) the resulting image will be forced to a 24-bit PNG format. This is notgenerally recommended, unless the source image is also a PNG.
  • background: If the radius is also set, this is used to specify a background color to fill the corners with. For example, if you wanted an image on a bright red background to have corners with a 10-pixel radius: {{ image_url|imagesize({radius:10,background:"#ff0000"}) }}
  • quality: Sets the output quality for JPEG files (ignored for other formats). This should be a number between 0 and 100, the default is 75.

in

Returns true if the value is contained within another one.

{# returns true #}
 
{{ 1|in([1, 2, 3]) }}
 
{{ 'cd'|in('abcde') }}

You can use this filter to perform a containment test on strings, arrays, or objects implementing the Traversable interface.

The in operator is a syntactic sugar for the in filter:

{% if 1 in [1, 2, 3] %}
  TRUE
{% endif %}
 
{# is equivalent to #}
 
{% if 1|in([1, 2, 3]) %}
  TRUE
{% endif %}

join

The join filter returns a string which is the concatenation of the strings in the sequence. The separator between elements is an empty string per default, you can define it with the optional parameter:

{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
 
{{ [1, 2, 3]|join }}
{# returns 123 #}

keys

The keys filter returns the keys of an array. It is useful when you want to iterate over the keys of an array:

{% for key in array|keys %}
    ...
{% endfor %}

length

The length filters returns the number of items of a sequence or mapping, or the length of a string.

{{ posts|length }}

lower

The lower filter converts a value to lowercase.

{{ title|lower }}

markdown

The markdown filter runs a markdown conversion on the variable and returns html. It is useful with markdown custom types.

{{ this.custom.content.markdown|markdown }}

odd

The odd filter returns true if the given number is odd, false otherwise:

{{ var|odd ? 'odd' : 'even' }}

range

Returns a list containing a sequence of numbers. The left side of the filter represents the low value. The first argument of the filter is mandatory and represents the high value. The second argument is optional and represents the step (which defaults to 1).

If you do need to iterate over a sequence of numbers:

{% for i in 0|range(10) %}
  * {{ i }}
{% endfor %}

The range filter works as the native PHP range function.

The .. operator (see above) is a syntactic sugar for the range filter (with a step of 1):

{% for i in 0|range(10) %}
  * {{ i }}
{% endfor %}
 
{# is equivalent to #}
 
{% for i in 0..10 %}
  * {{ i }}
{% endfor %}

regex_replace

regex_replace is similar to the replace filter, except the first paramter should be a regular expression to match, rather than a plain string. You should not surround the regular expression with forward slashes. Optionally, you may also include the modifier flags as a third parameter, and a maximum number of matches to return as the fourth parameter.

{# Standardize the company's name in text #}
{{ entry_text|regex_replace('nmc|new media( campaigns)?','New Media Campaigns','i') }}

For security reasons, you cannot use the "e" (evaluate) modifier.

replace

The replace filter takes two parameters. All occurrences of the first parameter in the string being filtered will be replaced with the second parameter.

{{ "New Media Campaigns uses HiFi."|replace("HiFi","the awesomest CMS ever") }}

reverse

The reverse filter reverses an array or an object if it implements the Iterator interface.

{{ arr|reverse }}

safe

The safe filter marks the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.

{% autoescape on }
  {{ var|safe }} {# var won't be escaped #}
{% autoescape off %}

shuffle

Randomly sort items within an array.

{% set arr as [1,2,3,4,5] %}
{% set newArr as arr|shuffle %}

sort

The sort filter sorts an array.

{{ arr|sort }}

striptags

The striptags filter strips SGML/XML tags and replace adjacent whitespace by one space.

{{ var|striptags }}

substr

The substr filter outputs a part of the supplied string, beginning at a particular character and optionally stopping after a certain length.

{{ "This is a string"|substr(3,6) }}
{# Returns "s is a" #}

You can also use a negative number for the first argument to start that number of characters from the end of the string.

title

The title filter returns a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.

{{ page.title|title }}

trim

The lower filter converts a value to lowercase.

{{ title|lower }}

truncate

The truncate filter trims the given string down to the specified length. It can optionally append another string to end.

{{ my_variable|truncate(30,'... Read more.') }}
{# Returns "This is my strin... Read more." #}

upper

The lower filter converts a value to uppercase.

{{ title|upper }}

helpCan't find your answer here? Head to our discussion board to get help from our developers and a growing community of HiFi users.