2 Straight Include template tag by @HenrikJoreteg
4 Django templates don't give us any way to escape template tags.
6 So if you ever need to include client side templates for ICanHaz.js (or anything else that
7 may confuse django's templating engine) You can is this little snippet.
9 Just use it as you would a normal {% include %} tag. It just won't process the included text.
11 It assumes your included templates are in you django templates directory.
15 {% load straight_include %}
17 {% straight_include "my_icanhaz_templates.html" %}
22 from django import template
23 from django.conf import settings
26 register = template.Library()
29 class StraightIncludeNode(template.Node):
30 def __init__(self, template_path):
31 for template_dir in settings.TEMPLATE_DIRS:
32 self.filepath = '%s/%s' % (template_dir, template_path)
33 if os.path.exists(self.filepath):
36 raise IOError("cannot find %s in %s" % (template_path, str(TEMPLATE_DIRS)))
38 def render(self, context):
39 fp = open(self.filepath, 'r')
45 def do_straight_include(parser, token):
47 Loads a template and includes it without processing it
51 {% straight_include "foo/some_include" %}
54 bits = token.split_contents()
56 raise template.TemplateSyntaxError("%r tag takes one argument: the location of the file within the template folder" % bits[0])
59 return StraightIncludeNode(path)
62 register.tag("straight_include", do_straight_include)