Filters - Modifying Variables In Twig Templates

Last updated on
29 August 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.

{{ 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


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.

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


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

{% 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:

{{ var1|t }}
{{ var1|placeholder }}
{% trans %}{{ var1 }}{% endtrans %}

Additional filters


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


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


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


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.


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.


The safe_join filter joins several strings together with a supplied separator. See TwigExtension::safeJoin().

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


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.

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