A custom suite of Twig filters for HTML manipulation
Explore the docs » · Report Bug · Request Feature
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.
- PHP >= 7.4 (tested up to 8.5)
- Composer >= 2.0
Install the package via composer:
composer require ocubom/twig-html-extensionRegister 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;
}
});See the open issues for a list of proposed features (and known issues).
Reach out to the maintainer at GitHub Issues.
Oscar Cubo Medina — https://ocubom.github.io
For a full list of all authors and contributors, see the contributors page.
Distributed under the MIT License.
See LICENSE for more information.