How to Trim texts in Twig - Drupal 8

Posted on September 20, 2016

Trim down text field with rich texts in Drupal 8 twig.

In Twig templating, text can be trimmed down using the slice filter.

However in case of rendering a text field of a content type we will have to strip out the attributes of the field data.

Directly putting the twig variable of the field will not yeild any result. Since it’s a rendered array alogn with the attributes provided by Drupal.

We need the rendered output from the field variable. Once we have the rendered output, strip out the HTML tags to get the clean raw text out of the field.

{% set text = content.body|render|striptags %}

Now, that we have the raw text out of the field value, we can use the twig slice filter to get the trimmed result with ellipses.

{{ text|length > 100 ? text|slice(0, 100) ~ '...' : text }}

It’s advisible to truncate strings with html is generally a bad idea due to having potential unclosed or malformed tags.