Skip to main content

Simple Django template loader that minifies html output.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-spaceless-templates-1.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-spaceless-templates-1.0.2.tar.gz.

File metadata

  • Download URL: django-spaceless-templates-1.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-spaceless-templates-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7a105558b9982e122eade5db445de8e8304dfc3124443ba385e503e1dfa784e2
MD5 095f08d2c4a1d8d20e1ba434bfb220f9
BLAKE2b-256 7c94ec50e47664ef38d2d60191ae7b5eb75f2e2d6390ba351e3120e23e7bd545

See more details on using hashes here.

File details

Details for the file django_spaceless_templates-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_spaceless_templates-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_spaceless_templates-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 09d952979f9b4d6090428eb611395b964890af0bf362b80c4e2036db81c0f29d
MD5 80570812aaa0d97500c500884c91ed91
BLAKE2b-256 fac67ecfdf7f37cfe6b63f3478a502b59e67e0d8f47c08c24d14abb60c2d40f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page