Skip to content

Instantly share code, notes, and snippets.

@risent
Forked from HenrikJoreteg/straight_include.py
Created March 7, 2012 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risent/1993819 to your computer and use it in GitHub Desktop.
Save risent/1993819 to your computer and use it in GitHub Desktop.
A Django Template tag for including files that you don't want to parse as templates
"""
Straight Include template tag by @HenrikJoreteg
Django templates don't give us any way to escape template tags.
So if you ever need to include client side templates for ICanHaz.js (or anything else that
may confuse django's templating engine) You can is this little snippet.
Just use it as you would a normal {% include %} tag. It just won't process the included text.
It assumes your included templates are in you django templates directory.
Usage:
{% load straight_include %}
{% straight_include "my_icanhaz_templates.html" %}
"""
from django import template
from django.conf import settings
register = template.Library()
class StraightIncludeNode(template.Node):
def __init__(self, template_path):
self.filepath = '%s/%s' % (settings.TEMPLATE_DIRS[0], template_path)
def render(self, context):
fp = open(self.filepath, 'r')
output = fp.read()
fp.close()
return output
def do_straight_include(parser, token):
"""
Loads a template and includes it without processing it
Example::
{% straight_include "foo/some_include" %}
"""
bits = token.split_contents()
if len(bits) != 2:
raise template.TemplateSyntaxError("%r tag takes one argument: the location of the file within the template folder" % bits[0])
path = bits[1][1:-1]
return StraightIncludeNode(path)
register.tag("straight_include", do_straight_include)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment