truncate
The truncate filter shortens a string to a specified length.
{{ content|truncate(100) }}
If your content contains HTML you will want to use the truncate_html filter instead as the truncate filter may result in broken HTML.
Arguments
The truncate filter has the following signature.
truncate(length, append)
Argument | Description |
---|---|
length | The length to truncate the string to. This parameter is required. To truncate a string to 100 characters you would do the following: {{ content|truncate(100) }} If the string is less than the number of characters specified then no truncation occurs. |
append | The character to append to the end of the truncated string. By default the character is … "…". If a value is not passed then the default character is used. Examples: {{ content|truncate(200, '>') }} |
Assignment
As with all filters, you can use the filter when assigning a value to another variable.
{% set shortContent = content|truncate(50) %}