truncate_html

The truncate_html filter shortens a string to a specified length and ensures that the HTML does not get broken.

{{ content|truncate_html(100) }}

Arguments

The truncate_html filter has the following signature.

truncate_html(length, append)

Parameter Description
length

The length to truncate the string to. 

This argument is required.

To truncate a string to 100 characters you would do the following:

{{ content|truncate_html(100) }}

Since HTML is preserved, the number of characters may not be exactly the number of characters submitted but it will be very close.

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_html(200, '>') }}

{% set link = '<a href="' ~ post.url ~ '">Read more&hellip;</a>' %}
{{ post.abstract|truncate_html(200, link) }}

Assignment

As with all filters, you can use the filter when assigning a value to another variable.

{% set shortContent = content|truncate_html(50) %}

< Back to the list of filters