Filters - Modifying Variables In Twig Templates

Last updated on
22 March 2017

Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

Example:
{{ content|safe_join(", ")|lower }}

You may have to render an item before you filter it:
{{ item|render|filter }}

Twig comes with many filters built into it, which you can review in their official documentation. Drupal has a variety of filters native to it.

Drupal Specific Filters

These are declared in TwigExtension::getFilters().

Translation filters

trans

This filter (alternatively, t) will run the variable through the Drupal t() function, which will return a translated string. This filter should be used for any interface strings manually placed in the template that will appear for users.

Example:
<a href="{{ url('<front>') }}" title="{{ 'Home'|t }}" rel="home" class="site-logo"></a>

placeholder

This filter escapes content to HTML and formats it using drupal_placeholder(), which makes it display as emphasized text.

Example:
{% trans %}Submitted on {{ date|placeholder }}{% endtrans %}

Unsafe translation

Some patterns are unsafe and should not be used because they pass a variable directly to translation. This not only inflates the list of strings for translation but is also a potential vulnerability, particularly if the output can be entered by a user. Some examples of improper translation:

{# DO NOT DO THIS #}
{{ var1|t }}
{{ var1|placeholder }}
{% trans %}{{ var1 }}{% endtrans %}

Additional filters

clean_class

This filter prepares a string for use as a valid HTML class name. See Html::getClass()

clean_id

This filter prepares a string for use as a valid HTML ID. See Html::getID()

format_date

This filter prepares a timestamp for use as a formatted date string. See DateFormatter::format()

raw

This filter should be avoided whenever possible, particularly if you're outputting data that could be user-entered. See this page for more information on auto-escape in Drupal 8.

render

This filter is a wrapper for the render() function. It takes a render array and outputs rendered HTML markup. This can be useful if you want to apply an additional filter (such as stripping tags), or if you want to make a conditional based on the rendered output (for example, if you have a non-empty render array that returns an empty string). It also can be used on strings and certain objects, mainly those implementing the toString() method.

safe_join

The safe_join filter joins several strings together with a supplied separator. See twig_drupal_join_filter.

Example:
{{ items|safe_join(', ') }}
This will output each string in the items variable concatenated together with a comma separating each item.

without

The without filter creates a copy of the renderable array and removes child elements by key specified through arguments passed to the filter. The copy can be printed without these elements. The original renderable array is still available and can be used to print child elements in their entirety in the twig template. See twig_without.

Example:
{{ content|without('links') }}
This prints everything in the content variable except content.links.