Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Created December 15, 2010 16:06
Show Gist options
  • Save HenrikJoreteg/742160 to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/742160 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)
@fadlee
Copy link

fadlee commented Nov 1, 2014

from django import template, conf

register = template.Library()

@register.simple_tag
def raw_include(path):
    import os.path

    for template_dir in conf.settings.TEMPLATE_DIRS:
        filepath = '%s/%s' % (template_dir, path)
        if os.path.isfile(filepath):
            break

    fp = open(filepath, 'r')
    output = fp.read()
    fp.close()
    return output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment