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. This is an update of django-template-minifier that works with django 1.10.

Things to note:

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',
          ],
      },
  },
]

Be happy having less spaces and new lines in Your templates!

Advanced usage:

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> or .js templates. In some cases, if You are using lot of {% if %} there, it can comment out }; or }, for example:

// comment something - !!it's evil!!
{% if %}
function name(){
}
{% endif %}

Use /* */ instead

/* comment something - it's nice and clean <3! */
{% if %}
function name(){
}
{% endif %}

Or just set TEMPLATE_MINIFIER_TEMPLATE_TAGS = False

  • Don’t use multiline {% blockquote %} without parameter trimmed. Otherwise your blockquote translations won’t be translated. Correct usage:

{% blockquote trimmed %}
    My paragraph...
{% blockquote %}

Running Tests:

(myenv) $ pip install -e .
(myenv) $ python ./runtests.py

Check package:

python -m build; python -m twine check dist/*

To do:

  • {% new_line %} template_tag

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.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

django_spaceless_templates-1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-spaceless-templates-1.0.tar.gz
  • Upload date:
  • Size: 4.8 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.tar.gz
Algorithm Hash digest
SHA256 964b9469cee4ac90fb2f0b5408a12a1aaf5111549d60eee81d2eb4ad2ce51182
MD5 d0589b196a571f8d1c9c485fdd14c841
BLAKE2b-256 2ff750a10942efe562122bfc4310875d227cedc34323d9d615e4d1cf8b92a0f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_spaceless_templates-1.0-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-py3-none-any.whl
Algorithm Hash digest
SHA256 95a5883f579ac899e548bfb117ee82823f300adad872a519f673a6253e79a97e
MD5 253d67ce5d4f4a6fb6de4b06eeed562b
BLAKE2b-256 6e80cf21e57d9f36780fb1373dc44edb15f34170bd1db0c33618a736fb9a4de8

See more details on using hashes here.

Supported by

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