Filters - Changing variables in Twig templates
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 together. The output of one filter is passed to the next.
Example:
{{ content|safe_join(", ")|lower }}
You may need to render an element before filtering it:
{{ item|render|filter }}
Twig comes with many built-in filters which you can review in their official documentation. Drupal provides many custom filters as well.
Drupal-Specific Filters
They are declared in TwigExtension::getFilters().
Translation Filters
trans
This filter (or t) will pass a variable through Drupal’s t() function, which returns a translated string. This filter should be used for any interface strings manually placed in a template that will be shown to users.
Example:
placeholder
This filter escapes content in HTML and formats it using drupal_placeholder(), allowing highlighted text display.
Example:
{% trans %}Submitted on {{ date|placeholder }}{% endtrans %}
Unsafe Translation
Some template usages are unsafe and should be avoided, as they pass a variable directly to translation. This not only inflates the translation string list but also poses a vulnerability, especially if the output is user-generated. Examples of incorrect usage:
{# DO NOT DO THIS #} {{ var1|t }} {{ var1|placeholder }} {% trans %}{{ var1 }}{% endtrans %}
Additional Filters
clean_class
Prepares a string for use as a valid HTML class name. See Html::getClass()
clean_id
Prepares a string for use as a valid HTML ID. See Html::getID()
format_date
Formats a timestamp into a date string. See DateFormatter::format()
raw
Should be avoided where possible, especially when outputting user-generated content. See this page for more information on auto-escaping in Drupal 8.
render
A wrapper for the render() function. It accepts a render array and outputs rendered HTML markup. Useful for applying additional filters (e.g., tag parsing), or when making conditions based on the rendered output. Also works for strings and objects with a toString() method.
safe_join
The safe_join filter joins multiple strings together using a specified delimiter. See TwigExtension::safeJoin().
Example:
{{ items|safe_join(', ') }}
This will output each string in the items variable, joined by a comma.
without
The without filter creates a copy of a render array and removes child elements by the key specified in the arguments passed to the filter. The copy can be printed without those elements. The original render array is still available and can be used to print all children in the Twig template. See twig_without.
Examples:
{{ content|without('links') }}
This prints everything in the content variable except content.links.
{{ content|without('links', 'field_some_data') }}
This example excludes two elements from rendering: “links” and “field_some_data”.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.