md-php / bridge-twig-html-truncation
Provides bridge between Twig and HTML truncation library
Package info
github.com/md-php/bridge-twig-html-truncation
pkg:composer/md-php/bridge-twig-html-truncation
Requires
- php: ^7.1 || ^8.0
- md-php/html-truncation: ^1.0 || ^2.0
- symfony/polyfill-php83: ^1.30
- twig/twig: ^2.12.5 || ^3.0
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^9.6
This package is not auto-updated.
Last update: 2026-04-22 18:42:48 UTC
README
md-php/bridge-twig-html-truncation is a PHP component that brings md-php/html-truncation functionality into Twig component and provides additional Twig filter and function to truncate HTML code.
Architecture overview
Install
composer require md-php/bridge-twig-html-truncation
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:
{{ "<p>some long html code</p>"|truncate_html_with_text_length(4) }}
Filter usage with custom ending example:
{{ "<p>some long html code</p>"|truncate_html_with_text_length(4, '$') }}
Read documentation for more examples.
Documentation
Read documentation with examples: https://development.md.land/php/md-bridge-twig-html-truncation/
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
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 {} \;