twig-truncate-extension

UNIX name Owner Status
twig-truncate-extension 7x stable
Version Compatible with
N/A N/A
Twig Extension to truncate nested HTML, safely!

Twig Truncation Extension

This extension attempts to solve a common problem, namely that of truncating HTML content based on the number of characters and on the number of words contained.

Other libraries we have tried attempt to manipulate the HTML content by means of regular expressions which is liable to break, and always seems to at the worst times, hence the need for this extension. This leverages the power of DOMDocument to safely truncate even the most complex of nested HTML documents.

Truncating on word count

{% set html %}

Test heading!



  • Hello world

  • Foo bar

  • Lorem Ipsum


{% endset html %}
{{ html|truncate_words(5) }}

Running this returns:

Test heading!



  • Hello world

  • Foo

Truncating on character count

{% set html %}

Test heading!



  • Hello world

  • Foo bar

  • Lorem Ipsum


{% endset html %}
{{ html|truncate_letters(20) }}

Whereas running the above returns the following:

Test heading!



  • Hello wo

No news yet.

This project has no reviews yet. Be the first one to review it!

No forum messages yet.