django-spaceless-templates

Simple Django template loader that minifies html output.


Keywords
django, template, minification, html, minifier
License
MIT
Install
pip install django-spaceless-templates==1.0.2

Documentation

Django Spaceless Templates

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/*