### Install Project Dependencies Source: https://github.com/akankov/twig-compress-html/blob/main/CONTRIBUTING.md Run this command in the repository root to install all necessary dependencies for development. ```bash composer install ``` -------------------------------- ### Install dependencies and run tests Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Execute these commands to set up the project for testing and run the unit tests. ```sh composer install vendor/bin/phpunit ``` -------------------------------- ### Plain Twig Setup with HtmlMinExtension Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Initialize Twig environment and add the HtmlMinExtension. Ensure HtmlMinRuntime is configured with an instance of HtmlMin. ```php use Akankov\HtmlMin\HtmlMin; use Akankov\TwigCompressHtml\HtmlMinExtension; use Akankov\TwigCompressHtml\HtmlMinRuntime; use Twig\Environment; use Twig\Loader\FilesystemLoader; use Twig\RuntimeLoader\FactoryRuntimeLoader; $twig = new Environment(new FilesystemLoader(__DIR__.'/templates')); $twig->addExtension(new HtmlMinExtension()); $twig->addRuntimeLoader(new FactoryRuntimeLoader([ HtmlMinRuntime::class => static fn () => new HtmlMinRuntime(new HtmlMin()), ])); ``` -------------------------------- ### Symfony Controller Example Source: https://context7.com/akankov/twig-compress-html/llms.txt Example of how to use the Twig Environment in a Symfony controller to render a template that utilizes the htmlmin filter or tag. ```php use Twig\Environment; class PageController { public function __construct(private readonly Environment $twig) {} public function index(): Response { // layout.html.twig uses {% htmlmin %} or |html_min — no extra wiring needed. return new Response($this->twig->render('layout.html.twig', ['title' => 'Home'])); } } ``` -------------------------------- ### Install twig-compress-html with Composer Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Use this command to add the package to your project dependencies. ```sh composer require akankov/twig-compress-html ``` -------------------------------- ### Bootstrap Plain Twig with HtmlMinExtension Source: https://context7.com/akankov/twig-compress-html/llms.txt Set up a plain Twig environment with the HtmlMinExtension and HtmlMinRuntime for inline HTML minification. ```php use Akankov\HtmlMin\HtmlMin; use Akankov\TwigCompressHtml\HtmlMinExtension; use Akankov\TwigCompressHtml\HtmlMinRuntime; use Twig\Environment; use Twig\Loader\FilesystemLoader; use Twig\RuntimeLoader\FactoryRuntimeLoader; $twig = new Environment(new FilesystemLoader(__DIR__ . '/templates'), [ 'autoescape' => 'html', 'cache' => '/tmp/twig-cache', 'strict_variables' => true, ]); $twig->addExtension(new HtmlMinExtension()); $twig->addRuntimeLoader(new FactoryRuntimeLoader([ HtmlMinRuntime::class => static fn () => new HtmlMinRuntime(new HtmlMin()), ])); // templates/page.html.twig // {{ rawHtml|html_min }} $rawHtml = "
Hello
"; $output = $twig->render('page.html.twig', ['rawHtml' => $rawHtml]); // Output:Hello
echo $output; ``` -------------------------------- ### Default Bundle Configuration Source: https://context7.com/akankov/twig-compress-html/llms.txt This is the default configuration for the bundle, showing all available options. ```yaml akankov_twig_compress_html: remove_comments: true # strips HTML comments sum_up_whitespace: true # collapses whitespace runs optimize_attributes: true # rewrites boolean/redundant attributes sort_html_attributes: false # leave attribute order as-is remove_omitted_quotes: false # keep attribute quotes ``` -------------------------------- ### Run Full CI Pipeline Source: https://github.com/akankov/twig-compress-html/blob/main/CONTRIBUTING.md Execute the complete continuous integration pipeline to ensure all checks pass before submitting changes. ```bash make ci ``` -------------------------------- ### Run Local Development Checks Source: https://github.com/akankov/twig-compress-html/blob/main/CONTRIBUTING.md Execute these commands to perform various checks on your local development environment. These include running tests, static analysis, and code formatting checks. ```bash vendor/bin/phpunit ``` ```bash vendor/bin/phpstan analyse ``` ```bash vendor/bin/php-cs-fixer fix --dry-run --diff ``` ```bash vendor/bin/rector process --dry-run ``` ```bash vendor/bin/phan --allow-polyfill-parser ``` -------------------------------- ### Symfony Bundle Configuration for Auto-registration Source: https://context7.com/akankov/twig-compress-html/llms.txt Configure the AkankovTwigCompressHtmlBundle in Symfony applications for automatic registration of Twig extensions and runtimes. ```php // config/bundles.php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], Akankov\TwigCompressHtml\Bundle\AkankovTwigCompressHtmlBundle::class => ['all' => true], ]; ``` -------------------------------- ### {% htmlmin %}...{% endhtmlmin %} Block Tag Source: https://context7.com/akankov/twig-compress-html/llms.txt Wraps a block of Twig markup (including dynamic variables and control structures) and minifies the fully-rendered output. Internally uses `ob_start()` / `ob_get_clean()` to capture the rendered buffer, then passes it to `HtmlMinRuntime::minify()`. ```APIDOC ## {% htmlmin %}...{% endhtmlmin %} Block Tag ### Description Wraps a block of Twig markup (including dynamic variables and control structures) and minifies the fully-rendered output. Internally uses `ob_start()` / `ob_get_clean()` to capture the rendered buffer, then passes it to `HtmlMinRuntime::minify()`. Variables are escaped by Twig's autoescape before minification, so user data is always safe. ### Usage Example ```twig {# templates/layout.html.twig #} {% htmlmin %}{{ item }}
{% endfor %} {% endhtmlmin %} ``` ### PHP Render Call Example ```php $output = $twig->render('layout.html.twig', [ 'title' => 'My Page', 'user' => '', // safely escaped to <script>... 'items' => ['Alpha', 'Beta'], ]); // Output (single line, minified): //Alpha
Beta
echo $output; ``` ``` -------------------------------- ### HtmlMinRuntime::minify() Source: https://context7.com/akankov/twig-compress-html/llms.txt The runtime class is the thin bridge between Twig and `akankov/html-min`. It implements `RuntimeExtensionInterface` and exposes a single `minify(string $html): string` method. You can instantiate it directly in non-Twig contexts or inject it via the service container. ```APIDOC ## HtmlMinRuntime::minify() ### Description The runtime class is the thin bridge between Twig and `akankov/html-min`. It implements `RuntimeExtensionInterface` and exposes a single `minify(string $html): string` method. You can instantiate it directly in non-Twig contexts or inject it via the service container. ### Method Signature ```php public function minify(string $html): string ``` ### Usage Example ```php use Akankov\HtmlMin\HtmlMin; use Akankov\TwigCompressHtml\HtmlMinRuntime; $htmlMin = new HtmlMin(); $runtime = new HtmlMinRuntime($htmlMin); $input = "Hello World
"; $output = $runtime->minify($input); // Output:Hello World
echo $output; ``` ``` -------------------------------- ### Configure HtmlMin options in Symfony Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Customize HtmlMin behavior by setting options in config/packages/akankov_twig_compress_html.yaml. Omitted keys retain their default upstream HtmlMin settings. ```yaml akankov_twig_compress_html: remove_comments: true sum_up_whitespace: true optimize_attributes: true sort_html_attributes: true remove_omitted_quotes: false ``` -------------------------------- ### Sum Up Whitespace Configuration Source: https://context7.com/akankov/twig-compress-html/llms.txt Configure the bundle to collapse multiple consecutive whitespace characters into a single space. This option is enabled by setting `sum_up_whitespace` to true. ```yaml akankov_twig_compress_html: sum_up_whitespace: true ``` -------------------------------- ### PHP-level Minification with HtmlMinRuntime::minify() Source: https://context7.com/akankov/twig-compress-html/llms.txt Directly use the HtmlMinRuntime::minify() method for HTML minification outside of Twig contexts or for custom integrations. ```php use Akankov\HtmlMin\HtmlMin; use Akankov\TwigCompressHtml\HtmlMinRuntime; $htmlMin = new HtmlMin(); $runtime = new HtmlMinRuntime($htmlMin); $input = "Hello World
"; $output = $runtime->minify($input); // Output:Hello World
echo $output; ``` -------------------------------- ### Register AkankovTwigCompressHtmlBundle in Symfony Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Add the bundle to your Symfony application's config/bundles.php file for automatic integration. ```php return [ // ... Akankov\TwigCompressHtml\Bundle\AkankovTwigCompressHtmlBundle::class => ['all' => true], ]; ``` -------------------------------- ### Remove HTML Comments Configuration Source: https://context7.com/akankov/twig-compress-html/llms.txt Configure the bundle to strip all HTML comments during minification. This is useful for production environments. ```yaml akankov_twig_compress_html: remove_comments: true ``` -------------------------------- ### Use htmlmin block tag in Twig Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Wrap HTML content within the {% htmlmin %}...{% endhtmlmin %} block to minify the rendered output. Variables within the block are escaped before minification. ```twig {% htmlmin %} {{ content }} {% endhtmlmin %} ``` -------------------------------- ### Minify HTML Section with {% htmlmin %} Block Tag Source: https://context7.com/akankov/twig-compress-html/llms.txt Use the {% htmlmin %}...{% endhtmlmin %} block tag in Twig templates to minify rendered sections, including dynamic variables and control structures. ```twig {# templates/layout.html.twig #} {% htmlmin %}{{ item }}
{% endfor %} {% endhtmlmin %} ``` ```php // PHP render call $output = $twig->render('layout.html.twig', [ 'title' => 'My Page', 'user' => '', // safely escaped to <script>... 'items' => ['Alpha', 'Beta'], ]); // Output (single line, minified): //Alpha
Beta
echo $output; ``` -------------------------------- ### Use html_min filter in Twig Source: https://github.com/akankov/twig-compress-html/blob/main/README.md Apply the html_min filter to a variable containing raw HTML to minify it. ```twig {{ rawHtml|html_min }} ``` -------------------------------- ### Twig Template with Whitespace Summation Source: https://context7.com/akankov/twig-compress-html/llms.txt Demonstrates the effect of the `sum_up_whitespace` option in a Twig template. Excessive whitespace within tags is reduced to a single space. ```twig {% htmlmin %}lots of space
{% endhtmlmin %} {# Output:lots of space
#} ``` -------------------------------- ### Twig Template with Comment Removal Source: https://context7.com/akankov/twig-compress-html/llms.txt Demonstrates the effect of the `remove_comments` option in a Twig template. Comments are removed when `remove_comments` is true. ```twig {# template.html.twig #} {% htmlmin %} Hello {% endhtmlmin %} {# With remove_comments: true → Hello #} {# With remove_comments: false → Hello #} ``` -------------------------------- ### html_min Twig Filter Source: https://context7.com/akankov/twig-compress-html/llms.txt Applies HTML minification to any string value in a Twig expression. The filter is marked 'is_safe => ['html']' so Twig does not double-escape the already-safe minified output. Best used when you have a pre-rendered HTML string or a variable containing markup. ```APIDOC ## html_min Twig Filter ### Description Applies HTML minification to any string value in a Twig expression. The filter is marked `is_safe => ['html']` so Twig does not double-escape the already-safe minified output. Best used when you have a pre-rendered HTML string or a variable containing markup. ### Usage Example ```twig {{ rawHtml|html_min }} ``` ### PHP Bootstrap Example ```php use Akankov\HtmlMin\HtmlMin; use Akankov\TwigCompressHtml\HtmlMinExtension; use Akankov\TwigCompressHtml\HtmlMinRuntime; use Twig\Environment; use Twig\Loader\FilesystemLoader; use Twig\RuntimeLoader\FactoryRuntimeLoader; $twig = new Environment(new FilesystemLoader(__DIR__ . '/templates'), [ 'autoescape' => 'html', 'cache' => '/tmp/twig-cache', 'strict_variables' => true, ]); $twig->addExtension(new HtmlMinExtension()); $twig->addRuntimeLoader(new FactoryRuntimeLoader([ HtmlMinRuntime::class => static fn () => new HtmlMinRuntime(new HtmlMin()), ])); $rawHtml = "Hello
"; $output = $twig->render('page.html.twig', ['rawHtml' => $rawHtml]); // Output:Hello
echo $output; ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.