https://github.com/zaczero/jinja2-htmlmin
Automatic HTML minification for Jinja2 templates
https://github.com/zaczero/jinja2-htmlmin
fastapi html html-minifier htmlmin jinja2 minification starlette templates
Last synced: 8 months ago
JSON representation
Automatic HTML minification for Jinja2 templates
- Host: GitHub
- URL: https://github.com/zaczero/jinja2-htmlmin
- Owner: Zaczero
- License: 0bsd
- Created: 2025-07-29T16:54:33.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-07-29T17:07:12.000Z (10 months ago)
- Last Synced: 2025-07-29T17:44:43.940Z (10 months ago)
- Topics: fastapi, html, html-minifier, htmlmin, jinja2, minification, starlette, templates
- Language: Python
- Homepage: https://pypi.org/p/jinja2-htmlmin
- Size: 0 Bytes
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jinja2-htmlmin
[](https://pypi.org/p/jinja2-htmlmin)
[](https://liberapay.com/Zaczero/)
[](https://github.com/sponsors/Zaczero)
Automatic HTML minification for Jinja2 templates.
## Why Use This?
- **Zero runtime overhead**: Minification happens once at load time, not on every render
- **Smaller HTML**: Reduced bandwidth and faster page loads
- **Drop-in compatibility**: Works with any Jinja2 loader (Flask, Django, FastAPI, etc.)
- **Broad Python support**: Compatible with Python 3.9+
- [**Semantic Versioning**](https://semver.org): Predictable, reliable updates
- [**Zero-Clause BSD**](https://choosealicense.com/licenses/0bsd/): Public domain, use freely anywhere
## Installation
```bash
pip install jinja2-htmlmin
```
## Quick Start
```python
from jinja2 import Environment, FileSystemLoader
from jinja2_htmlmin import minify_loader
# Wrap any Jinja2 loader
# See https://htmlmin.readthedocs.io/en/latest/reference.html for options
env = Environment(
loader=minify_loader(
FileSystemLoader("templates"),
remove_comments=True,
remove_empty_space=True,
remove_all_empty_space=True,
reduce_boolean_attributes=True,
)
)
# Rendered HTML is automatically minified
html = env.get_template('index.html').render(title='My Page')
```
## How It Works
1. Template loads normally through your existing loader
2. Jinja2 syntax is temporarily protected during minification
3. HTML content is minified using [htmlmin2](https://github.com/wilhelmer/htmlmin)
4. Jinja2 syntax is restored and template compiles as usual
5. Jinja2's built-in caching means this happens only once per template