Other Filters

date

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

{{ post.published_at|date("m/d/Y") }}

The date filter accepts any date format supported by DateTime and DateTime instances. For instance, to display the current date, filter the word “now”:

{{ "now"|date("m/d/Y") }}

date_modify

The date_modify filter modifies a date with a given modifier string:

{{ post.published_at|date_modify("+1 day")|date("m/d/Y") }}

The date_modify filter accepts strings (it must be in a format supported by the strtotime function) or DateTime instances. You can easily combine it with the date filter for formatting.

format

The format filter formats a given string by replacing the placeholders (placeholders follows the printf notation):

{{ "I like %s and %s."|format(foo, "bar") }}

{# returns I like foo and bar. (if the foo parameter equals to the foo string) #}

replace

The replace filter formats a given string by replacing the placeholders (placeholders are free-form):

{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}

{# returns I like foo and bar. (if the foo parameter equals to the foo string) #}

url_encode

The url_encode filter URL encodes a given string.

json_encode

The json_encode filter returns the JSON representation of a 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.

capitalize

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

upper

The upper filter converts a value to uppercase.

lower

The lower filter converts a value to lowercase.

striptags

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

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 #}

reverse

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

length

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

sort

The sort filter sorts an array.

default

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

{{ var|default('var is not defined') }}

{{ var.foo|default('foo item on var is not defined') }}

{{ ''|default('passed var is empty')  }}

Note

Read the documentation for the defined and empty tests below to learn more about their semantics.

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 %}

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.

Note

Internally, escape uses the PHP htmlspecialchars function.

raw

The raw filter marks the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it.

{% autoescape true %}
  {{ var|raw }} {# var won't be escaped #}
{% endautoescape %}

merge

The merge filter merges an array or a hash with the value:

{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}

{% set items = items|merge({ 'peugeot': 'car' }) %}

{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}