bluetel-solutions/twig-truncate-extension

Twig Extension to truncate nested HTML, safely!

Maintainers

👁 alexwilson

Package info

github.com/Bluetel-Solutions/twig-truncate-extension

Issues

pkg:composer/bluetel-solutions/twig-truncate-extension

Statistics

Installs: 103 310

Dependents: 0

Suggesters: 0

Stars: 11

v0.1.3 2016-01-18 22:41 UTC

Requires

Requires (Dev)

None

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 6c94f78c1c4e55d909b94648f36a5254291be834

twigtruncatetwig extension

This package is not auto-updated.

Last update: 2026-06-25 03:59:20 UTC


README

👁 Build Status

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 %}
 <h1>Test heading!</h1>
 <ul>
 <li>Hello world</li>
 <li>Foo bar</li>
 <li>Lorem Ipsum</li>
 </ul>
{% endset html %}
{{ html|truncate_words(5) }}

Running this returns:

<h1>Test heading!</h1>
<ul>
 <li>Hello world</li>
 <li>Foo</li>
</ul>

Truncating on character count

{% set html %}
 <h1>Test heading!</h1>
 <ul>
 <li>Hello world</li>
 <li>Foo bar</li>
 <li>Lorem Ipsum</li>
 </ul>
{% endset html %}
{{ html|truncate_letters(20) }}

Whereas running the above returns the following:

<h1>Test heading!</h1>
<ul>
 <li>Hello wo</li>
</ul>