Skip to main content

Unobtrusive jinja2 integration in Django

Project description

© 2014 Thomas Khyn

Unobtrusive jinja2 integration in Django

Freely inspired from django-jinja and django-jinja2, as none of them met all my expectations!

Compatible with django 1.4 to 1.7 and relevant python versions (2.7 to 3.4).

Usage

  • Install djinga using the method of your choice

  • Add ‘djinga’ to your INSTALLED_APPS

  • Change the TEMPLATE_LOADERS settings to:

    TEMPLATE_LOADERS = (
       'djinga.loaders.FileSystemLoader',
       'djinga.loaders.AppLoader',
    )
  • Add the relevant settings for jinja2

How it works

By default, a template will be rendered using Django’s built-in template engine if it has a .html, .htm, .djhtml or .djhtm file extension. If it has a .jjhtml or .jjhtm file extension, it will be rendered by Jinja2, using the setting values provided in django’s setting module.

Settings

JINJA2_DJ_EXTS

The file extensions for templates that should be rendered with Django’s internal template engine.

Defaults to ('html', 'htm', 'jjhtml', 'jjhtm')

JINJA2_JJ_EXTS

The file extensions for templates that should be rendered with Jinja2.

Defaults to ('jjhtml', 'jjhtm')

JINJA2_CONDITION

A function taking as sole argument the path of the template file and returning True if the file should be rendered with Jinja2. Defaults to a function returning True if the extension is in JINJA2_JJ_EXTS

JINJA2_EXTENSIONS

The extensions to be loaded by jinja2. Some extensions are shipped with djinga under djinga.ext.*

JINJA2_ENV_ARGS

The jinja2 environment’s constructor keyword arguments as a dictionary.

JINJA2_GLOBALS

The jinja2 globals as a dictionary.

JINJA2_FILTERS

The jinja2 filters as a dictionary.

Extensions and template tags

Built-in Jinja2 extensions

Djinga comes with several Jinja2 extensions

djinga.ext.static

Provides a {% static 'path' %} tag to refer to Django’s staticfiles directory

djinga.ext.css

Provides a {% css 'rel/path/to/file.css' %} tag that generates a HTML link element refering to the css file located at a relative path in a css directory. The css directory’s path can be defined relatively to Django’s staticfiles directory through the setting JINJA2_STATIC_CSS

djinga.ext.js

Same as djinga.ext.css but generates a HTML script element refering to a javascript file. The js directory’s relative path can be set through the setting JINJA2_STATIC_JS

djinga.ext.media

Simply concatenates django’s MEDIA_URL to the argument provided

djinga.ext.django

Provides a {% django %}{% enddjango %} tag to include django template language in a jinja2 template. For this tag to work, the django.core.context_processors.request context processor must be enabled.

djinga.ext.url

Provides a tag for URL reversing, similar to the django templates one.

Other Jinja2 extensions

djinga.ext.htmlcompress.HTMLCompress / SelectiveHTMLCompress

© 2011 Armin Ronacher. Eliminates useless whitespace at template compilation time without extra overhead.

Django template tags

The following tags are available in any django template explicitly loading djinga_tags using the {% load djinga_tags %} statement.

extends

Overrides the standard {% extends %} tag and enables it to refer to jinja2 files as well as normal django template files. While the template engine for the current file remains Django’s one, the template engine for the extended file can be either Jinja2 or Django, depending on the file extension (in JINJA2_DJ_EXTS or JINJA2_JJ_EXTS)

Management command

Djinga overrides the Django core management command makemessages to include the specific Jinja2 translation tags and ensure the strings marked for translation in Jinja2 templates appear in the translations dictionary.

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

djinga-1.1.1.zip (20.1 kB view hashes)

Uploaded Source

Built Distribution

djinga-1.1.1-py2.7.egg (36.5 kB view hashes)

Uploaded Source

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