Django application, providing simple template loader. It reduces HTML output in templates by stripping out whitespace characters between HTML and django template tags. With cached template loader, whitespace stripping is done only once during template compilation. This is more efficient than solutions based on {% spaceless %} tag or middleware minification.
This package is based on following unmaintained packages:
How much bandwidth does it save? Check data from real project:
Normal HTML |
109kB |
15kB gzipped |
Spaceless HTML |
67kB |
13kB gzipped |
Saved |
38 % |
12 % gzipped |
Installation
pip install django-spaceless-templates
Basic usage
Modify Your Django project settings’s module.
For production (note cached loader):
TEMPLATES = [
{
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'loaders': [
(
'django.template.loaders.cached.Loader', [
'django_spaceless_templates.loaders.filesystem.Loader',
'django_spaceless_templates.loaders.app_directories.Loader',
],
),
],
},
},
]
For development (each refresh reloads template):
TEMPLATES = [
{
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'loaders': [
'django_spaceless_templates.loaders.filesystem.Loader',
'django_spaceless_templates.loaders.app_directories.Loader',
],
},
},
]
Settings
Using modified settings You can:
turn on stripping only for templates with given extensions
TEMPLATE_MINIFIER_FILENAME_EXTENSIONS = ('.html', '.htm', )
turn off stripping for particular directories
TEMPLATE_MINIFIER_EXCLUDED_DIRS = ('admin/', )
turn off all stripping
TEMPLATE_MINIFIER = False # default = True
run Your own strip_function, which preprocess templates
TEMPLATE_MINIFIER_STRIP_FUNCTION = 'template_minifier.utils.strip_spaces_in_template'
use only in production
if DEBUG:
TEMPLATE_MINIFIER = False
Known issues
Don’t use // one line comments in your inline javascript <script> tags. Use /* */ instead:
// comment something - !!it's evil!! and cause the rest of JS code is commented out.
function name() {
}
/* comment something - it's nice and clean <3! */
function name() {
}
Don’t use multiline {% blockquote %} without parameter trimmed. Otherwise your blockquote translations won’t be translated. Correct usage:
{% blockquote trimmed %}
My paragraph...
{% blockquote %}
To preserve extra space use {{ " " }}:
<div>Text {{ " " }} {{ variable }}</div>
Running Tests
(myenv) $ pip install -e . (myenv) $ python ./runtests.py
Check package
python -m build; python -m twine check dist/*
Release files for django-spaceless-templates 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-spaceless-templates-1.0.2.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_spaceless_templates-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / django-spaceless-templates-1.0.2.tar.gz
| Download URL | django-spaceless-templates-1.0.2.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a105558b9982e122eade5db445de8e8304dfc3124443ba385e503e1dfa784e2
|
|
BLAKE2b-256 checksum How to use checksums |
7c94ec50e47664ef38d2d60191ae7b5eb75f2e2d6390ba351e3120e23e7bd545
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
|
Release files / django_spaceless_templates-1.0.2-py3-none-any.whl
| Download URL | django_spaceless_templates-1.0.2-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
09d952979f9b4d6090428eb611395b964890af0bf362b80c4e2036db81c0f29d
|
|
BLAKE2b-256 checksum How to use checksums |
fac67ecfdf7f37cfe6b63f3478a502b59e67e0d8f47c08c24d14abb60c2d40f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
|