bluetel-solutions/twig-truncate-extension
Twig Extension to truncate nested HTML, safely!
Maintainers
Package info
github.com/Bluetel-Solutions/twig-truncate-extension
pkg:composer/bluetel-solutions/twig-truncate-extension
Requires
- php: >=5.3.0
- antoligy/dom-string-iterators: v1.0.0
- twig/twig: >=v1.0.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 6c94f78c1c4e55d909b94648f36a5254291be834
- Alex Wilson <a.woop@ax.gy>
- Bluetel Solutions <developers.woop@bluetel.co.uk>
This package is not auto-updated.
Last update: 2026-06-25 03:59:20 UTC
README
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>
