ocubom / twig-html-extension
A custom suite of Twig filters for HTML manipulation
Requires
- php: >=7.4
- twig/twig: ^2.7|^3.0
- voku/html-min: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: *
- phpstan/phpstan: ^2.2
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- symfony/phpunit-bridge: ^4.4.9|^5.0.9|^6.0|^7.0|^8.0
- symfony/web-profiler-bundle: *
Suggests
- matthiasmullie/minify: Allows using Matthias Mullie's CSS & JS minifiers
- tedivm/jshrink: Allows using Robert Hafner's JS minifier
- tubalmartin/cssmin: Allows using Tubal Martin's CSS minifier (YUI CSS compressor)
- wikimedia/minify: Allows using Wikimedia's CSS & JS minifiers
README
A custom suite of Twig filters for HTML manipulation
Explore the docs » · Report Bug · Request Feature
About ocubom/twig-html-extension
ocubom/twig-html-extension is a custom suite of Twig extensions for HTML manipulation.
This suite started as an internal helper based on nochso/html-compress-twig to bring wyrihaximus/html-compress support to Twig 3.0. Over time, as several projects embedded and modified this class independently, development became fragmented and difficult to maintain. This repository unifies those efforts into a single, open-source package.
Starting in version 2.2.0, the engine was upgraded to directly use
voku/html-min, replacing the legacy compressor dependencies.
Full backward compatibility is maintained for legacy HtmlCompressRuntime.
Getting Started
Prerequisites
- PHP >= 7.4 (tested up to 8.5)
- Composer >= 2.0
Installation
Install the package via composer:
composer require ocubom/twig-html-extension
Usage
Register the extension and runtime loaders in your Twig environment:
use Twig\Environment; use Twig\RuntimeLoader\FactoryRuntimeLoader; use Ocubom\Twig\Extension\HtmlExtension; use Ocubom\Twig\Extension\HtmlAttributesRuntime; use Ocubom\Twig\Extension\HtmlMinifyRuntime; $twig = new Environment($loader); // Register the extension $twig->addExtension(new HtmlExtension()); // Register runtime loaders $twig->addRuntimeLoader(new FactoryRuntimeLoader([ HtmlAttributesRuntime::class => fn() => new HtmlAttributesRuntime(), HtmlMinifyRuntime::class => fn() => new HtmlMinifyRuntime(), ])); // You can also dynamically create a RuntimeLoader $twig->addRuntimeLoader(new class() implements RuntimeLoaderInterface { public function load($class) { if (HtmlAttributesRuntime::class === $class) { return new HtmlAttributesRuntime(); } if (HtmlMinifyRuntime::class === $class) { return new HtmlMinifyRuntime(); } return null; } });
Roadmap
See the open issues for a list of proposed features (and known issues).
Support
Reach out to the maintainer at GitHub Issues.
Authorship
Oscar Cubo Medina — https://ocubom.github.io
For a full list of all authors and contributors, see the contributors page.
License
Distributed under the MIT License.
See LICENSE for more information.