akankov/twig-compress-html
Composer 安装命令:
composer require akankov/twig-compress-html
包简介
Twig 3 extension wrapping akankov/html-min: provides an html_min filter and a {% htmlmin %}...{% endhtmlmin %} block tag, plus an optional Symfony bundle.
README 文档
README
twig-compress-html
A Twig 3 extension wrapping akankov/html-min — exposes an html_min filter and an {% htmlmin %}...{% endhtmlmin %} block tag, with an optional Symfony bundle for auto-registration.
Requirements
- PHP
8.3.* || 8.4.* || 8.5.* twig/twig^3.0akankov/html-min^2.9
Install
composer require akankov/twig-compress-html
Plain Twig usage
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()), ]));
Filter
{{ rawHtml|html_min }}
Block tag
{% htmlmin %}
<html>
<body>
{{ content }}
</body>
</html>
{% endhtmlmin %}
The tag captures rendered output (variables are escaped first by Twig's autoescape, then minified), so it's safe to interpolate user data inside.
Symfony usage
Register the bundle in config/bundles.php:
return [ // ... Akankov\TwigCompressHtml\Bundle\AkankovTwigCompressHtmlBundle::class => ['all' => true], ];
Optionally tune HtmlMin via config/packages/akankov_twig_compress_html.yaml:
akankov_twig_compress_html: remove_comments: true sum_up_whitespace: true optimize_attributes: true sort_html_attributes: true remove_omitted_quotes: false minify_inline_css: true local_domains: ['example.com']
The filter and tag become available in all templates automatically.
Configuration
Every config key is a snake_case mirror of a property on
Akankov\HtmlMin\Config\MinifierOptions; the bundle camel-cases them and builds
the options object that backs the shared HtmlMin service, so:
remove_comments: true # → MinifierOptions::$removeComments sum_up_whitespace: true # → MinifierOptions::$sumUpWhitespace local_domains: ['a.test'] # → MinifierOptions::$localDomains
All of the engine's options are accepted — the 27 boolean toggles plus the list
options local_domains, special_html_comments_starting_with,
special_html_comments_ending_with, special_script_tags, and
template_logic_syntax_in_special_script_tags. Any key you omit falls through
to the engine's own default, so the bundle never pins (or drifts from) those
defaults. This matches the surface exposed by the Laravel binding's
config/htmlmin.php.
Response minification (opt-in)
To minify whole text/html responses — not just {% htmlmin %} blocks — enable
the kernel.response listener. It is off by default (the bundle never adds
it to the response pipeline on its own), mirroring the Laravel binding's
MinifyHtmlResponseMiddleware:
akankov_twig_compress_html: minify_responses: true
Sub-requests, streamed responses, and non-text/html responses pass through
untouched, so it is safe in front of a mixed JSON / HTML application.
What "pass through" covers, precisely:
- Streamed responses (
StreamedResponse,BinaryFileResponse) are never buffered or minified — the listener only touches responses whose body it can read as a string. - Partial content (
206responses to range requests) is skipped by the same mechanism in practice — range responses are produced as binary-file responses; minifying a byte range of HTML would corrupt it. - ESI/SSI fragments: the listener runs only on the main request, so edge-side includes assembled by a proxy are minified per-fragment only if your proxy requests them as main requests against this app — in that case each fragment is valid standalone HTML and minifies safely. Fragments rendered as Symfony sub-requests pass through untouched.
Console command
With symfony/console installed, the bundle registers html-min:check — a
CI/dev smoke-check that minifies a file in memory and reports the byte
savings without writing to disk (mirrors the Laravel binding's Artisan
command):
bin/console html-min:check templates/page.html
# [OK] Reduced from 4.2 KB to 3.1 KB (-26.3%)
It exits 0 on success and 1 if the file cannot be read.
Versioning
This package follows Semantic Versioning. From 1.0.0 onward the public surface — the html_min filter, the {% htmlmin %} block tag, the Symfony bundle's config keys, and the opt-in response listener — is stable; breaking changes are reserved for a new major version. The underlying engine is tracked via a caret constraint (akankov/html-min: ^2.9), so it picks up engine minor/patch releases automatically.
Tests
composer install
vendor/bin/phpunit
# line coverage + floor enforcement (needs pcov or xdebug)
make coverage
License
MIT
akankov/twig-compress-html 适用场景与选型建议
akankov/twig-compress-html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54.33k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「twig」 「bundle」 「html」 「minify」 「compress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 akankov/twig-compress-html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 akankov/twig-compress-html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 akankov/twig-compress-html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
统计信息
- 总下载量: 54.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-30