Django Template "include_raw" tag

 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. "Save and Continue" keyboard command for admin, with autoscroll by jcushman 2 years ago
  2. Client-side django template with jQuery by ksjun 3 years, 7 months ago
  3. Server Side Cursors for Django's psycopg2 Backend by ryanbutterfield 11 months, 1 week ago
  4. YUI Loader as Django middleware by akaihola 3 years, 9 months ago
  5. Effective content caching for mass-load site using redirect feature by nnseva 7 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!!!

#

(Forgotten your password?)