hasenj blog

Integrating Jinja2 with django

Posted in webdev by hasen on 12/11/2008

(I have this posted on django snippets: http://www.djangosnippets.org/snippets/1112/ and it’s where you can view the code and download it)

Jinja2 is an alternative template system that can be plugged into django. It offers greator flexibility in presentation logic; if you find the django template system too restrictive, you should have a look at Jinja2 (The syntax is very similar).

In Jinja, you don’t need costum tags (most of the time), because you can call functions and pass them parameters too! The only problem is that you cannot “load” functions from the template, this “loading” must be done by the code that renders the template. Same goes for filters and tests.

If you need only two or three functions/filters, no problem, you can manually add them to the Environment object; but this method doesn’t really scale with real-life webapps.

This module/snippet offers a solution by allowing the user to define application-specific functions and filters and load them into the jinja2 environment automatically.
It also sets auto_escaping on (Jinja2 has it off by default).
It searches for templates in TEMPLATE_DIRS and also in the “templates” subfolder (if it exists) of each application folder.

The approach I’ve taken to specify functions and filters is like this:

To add test functions, put them in a file called “jtests.py” and let each function start with “is_test_name”; it will be available as “test_name” in your jinja2 environment, so you can test {% if var is test_name %}

The same idea applies for filters, put them in a file called “jfilters.py” and let each one start with “filter_name”, and it will be available as “name” in the environment, so you can say “{{var|name}}”

For functions, it’s a little different; you add them a file called “jtemp.py” (temp is my wacky abbreviation of template global functions), but they are put in a name space that’s named after the application. so if your application is called “app”, then any function you put in jtemp.py will be avaiable in the template environment as {{ app.function_name() }}

Here’s how to use this:

  1. Install Jinja2 (for a quick & dirty installation, you can just put the jinja2 folder in a folder that’s in PYTHONPATH)
  2. Save this python module somewhere and call it something meaningful (I have it as jinja.py in my project directory)
  3. Whenever you need to respond (an HttpResponse) with a template that’s rendered by jinja, import this module and call return jrespond( template_name, context )
  4. Any filters or functions you define in any of your applications will be readily available inside the template (see the documentation in code)
Advertisements
Tagged with: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: