PHPackages                             md-php/bridge-twig-html-truncation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. md-php/bridge-twig-html-truncation

ActiveLibrary[Templating &amp; Views](/categories/templating)

md-php/bridge-twig-html-truncation
==================================

Provides bridge between Twig and HTML truncation library

1.0.0(2mo ago)03↓100%MITPHPPHP ^7.1 || ^8.0CI passing

Since Apr 1Pushed 1mo agoCompare

[ Source](https://github.com/md-php/bridge-twig-html-truncation)[ Packagist](https://packagist.org/packages/md-php/bridge-twig-html-truncation)[ Docs](https://development.md.land/php/md-bridge-twig-html-truncation)[ RSS](/packages/md-php-bridge-twig-html-truncation/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

md-php/bridge-twig-html-truncation
==================================

[](#md-phpbridge-twig-html-truncation)

md-php/bridge-twig-html-truncation is a PHP component that brings [md-php/html-truncation](https://github.com/md-php/html-truncation) functionality into [Twig](https://github.com/twigphp/Twig) component and provides additional Twig filter and function to truncate HTML code.

Architecture overview
---------------------

[](#architecture-overview)

[![Architecture overview class diagram](docs/_static/architecture-overview.class-diagram.svg)](docs/_static/architecture-overview.class-diagram.svg)

Install
-------

[](#install)

```
composer require md-php/bridge-twig-html-truncation
```

Usage
-----

[](#usage)

```
# Initialize extension
$truncate  = new \MD\HTML\Action\Truncate();
$extension = new \MD\Bridge\Twig\HTML\Extension\TruncateExtension(truncate: $truncate);

# Initialize component
$loader = new \Twig\Loader\ArrayLoader(templates: []);  // ... just example loader
$twig   = new \Twig\Environment(loader: $loader);

# Usage
$twig->addExtension(extension: $extension);
```

Extension brings filter and function with the `truncate_html_with_text_length` name and next signature:

```
/**
 * Truncates HTML code to concrete length of text, taking care about correct removal of rest of elements and
 * element tags close.
 *
 * The content of element where given max length is reached truncated with adding `$ending` parameter content.
 * All next siblings elements and child are removed from the result.
 * Space characters in the begging or the in end of element text is not counted.
 *
 * @param  string $html      Source HTML code to truncate
 * @param  int    $maxLength Max length of text characters in HTML code to keep until truncation. Unsigned int.
 * @param  string $ending    Content to append to the truncated text, by default is ellipsis `...`
 * @return string            Truncated HTML code.
 *
 * @throws TruncateException
 */
function withTextLength(string $html, int $maxLength, string $ending = '...'): string;
```

Basic filter usage example:

```
{{ "some long html code"|truncate_html_with_text_length(4) }}
```

Filter usage with custom ending example:

```
{{ "some long html code"|truncate_html_with_text_length(4, '$') }}
```

Read documentation for more examples.

[Documentation](docs/index.md)
------------------------------

[](#documentation)

Read documentation with examples:

Integration with Symfony
------------------------

[](#integration-with-symfony)

Define services:

```
services:
    MD\HTML\Action\TruncateInterface:
        alias: MD\HTML\Action\Truncate
    MD\HTML\Action\Truncate: ~

    MD\Bridge\Twig\HTML\Extension\TruncateExtension:
        arguments:  # ... may be omitted, if autowiring is in use
            $truncate: '@MD\HTML\Action\TruncateInterface'
        tags:
            - name: twig.extension
```

and it's ready to use.

Development
-----------

[](#development)

Initial setup:

```
cp .env.dist .env  # ... change `.env` for your needs
```

Run checks (Psalm + PHPStan + PHPUnit):

```
for PHP_VERSION in '71' '80' '85'; do
  export PHP_VERSION="$PHP_VERSION"
  docker compose run -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --build --remove-orphans --env-from-file .env -e XDEBUG_MODE=coverage --entrypoint="bash -c 'composer install ; mv composer.lock composer.php$PHP_VERSION.lock'" run-tests
  docker compose run -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --build --remove-orphans --env-from-file .env -e XDEBUG_MODE=coverage --entrypoint="/usr/src/app/vendor/bin/phpunit --configuration /usr/src/app/phpunit.php$(( PHP_VERSION == 85 ? 80 : PHP_VERSION )).xml.dist --coverage-text" run-tests
  docker run -e "PHP_VERSION=$PHP_VERSION" -v "$PWD:/app" -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --rm -it ghcr.io/danog/psalm:latest /composer/vendor/bin/psalm --no-cache
  docker run -e "PHP_VERSION=$PHP_VERSION" -v "$PWD:/app" -v "$PWD/vendor${PHP_VERSION}:/app/vendor" --rm -it ghcr.io/phpstan/phpstan analyse --configuration phpstan$([ "$PHP_VERSION" = "71" ] && echo ".71" || echo "").neon
done;
```

Validate files:

```
composer validate --strict
find ./src ./tests -name "*.php" -exec php -l {} \;
```

[Changelog](changelog.md)
-------------------------

[](#changelog)

[License (MIT)](license.md)
---------------------------

[](#license-mit)

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance89

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

69d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ee307dc6bfc713606c213571f27ddb1143670b6d94754bb090995f33c8e78b46?d=identicon)[unsektor](/maintainers/unsektor)

---

Top Contributors

[![unsektor](https://avatars.githubusercontent.com/u/529297?v=4)](https://github.com/unsektor "unsektor (5 commits)")

---

Tags

html-truncatesolid-principlestruncationtwigtwightmltruncatetruncation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/md-php-bridge-twig-html-truncation/health.svg)

```
[![Health](https://phpackages.com/badges/md-php-bridge-twig-html-truncation/health.svg)](https://phpackages.com/packages/md-php-bridge-twig-html-truncation)
```

###  Alternatives

[twig/string-extra

A Twig extension for Symfony String

22349.2M200](/packages/twig-string-extra)[twig/markdown-extra

A Twig extension for Markdown

12115.5M114](/packages/twig-markdown-extra)[twig/intl-extra

A Twig extension for Intl

36667.2M317](/packages/twig-intl-extra)[symfony/ux-twig-component

Twig components for Symfony

21917.2M298](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1636.5M111](/packages/symfony-ux-live-component)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22919.7M79](/packages/twig-cssinliner-extra)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
