Login

Django Template "include_raw" tag

Author:
wwu.housing
Posted:
August 13, 2009
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

At WWU Housing, we started using the Tempest jQuery plugin for javascript templating, which has the same {{ var }} syntax as Django's templating.

We wanted to be able to use the same templates in our server-side python and our client-side js, so we had to have a way of including the unrendered template for the js. At the same time, for convenience, it had to be modular so we could push the same code from our dev- to our live-server and not worry about absolute paths (which is why the {% ssi %} tag did not work).

So the {% include_raw %} tag was born.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from django import template
from django.template.loaders.app_directories import load_template_source
 
register = template.Library()
 
def do_include_raw(parser, token):
    """ 
    Performs a template include without parsing the context, just dumps the template in.
    """
    bits = token.split_contents()
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]
 
    template_name = bits[1]
    if template_name[0] in ('"', "'") and template_name[-1] == template_name[0]:
        template_name = template_name[1:-1]
 
    source, path = load_template_source(template_name)
 
    return template.TextNode(source)
register.tag("include_raw", do_include_raw)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

joaodubas (on December 13, 2010):

It would be cool if the load_template_source was imported also from django.template.loaders.filesystem. Probably the line 18 could be changed to something like this:

from django.template.loaders import filesystem, app_directories
...
try:
    source, path = filesystem.load_template_source(template_name)
except filesystem.TemplateDoesNotExist:
    try:
        source, path = app_directories.load_template_source(template_name)
    except ad.TemplateDoesNotExist:
        raise app_directories.TemplateDoesNotExist, "The template %s, does not exist." % (template_name, )

Besides that, a great work!!!

#

macavity (on March 27, 2013):

This no longer works as load_template_source has been deprecated. Instead, use the Loader class:

from django.template.loaders.app_directories import Loader
...
# Replaces: source, path = load_template_source(template_name)
source, path = Loader().load_template_source(template_name)

Replace app_directories with filesystem as required according to https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types - or use a combination approach as joaodubas suggests.

#

Please login first before commenting.