Created
December 20, 2011 23:09
-
-
Save bohde/1503729 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django import template | |
| from django.conf import settings | |
| import pystache | |
| register = template.Library() | |
| class MustacheNode(template.Node): | |
| def __init__(self, template_path, attr=None): | |
| self.filepath = '%s/%s' % (settings.TEMPLATE_DIRS[0], template_path) | |
| self.attr = attr | |
| def render(self, context): | |
| fp = open(self.filepath, 'r') | |
| template = fp.read() | |
| fp.close() | |
| mcontext = context[self.attr] if self.attr else {} | |
| return pystache.render(template, mcontext) | |
| def do_mustache(parser, token): | |
| """ | |
| Loads a mustache template and render it inline | |
| Examples:: | |
| {% mustache "foo/some_include" %} | |
| {% mustache "foo/some_include" data %} | |
| """ | |
| bits = token.split_contents() | |
| if len(bits) not in [2,3]: | |
| raise template.TemplateSyntaxError("%r tag takes two arguments: the location of the template file, and the optional template context" % bits[0]) | |
| path = bits[1] | |
| path = path[1:-1] | |
| attrs = bits[2:] | |
| return MustacheNode(path, *attrs) | |
| register.tag("mustache", do_mustache) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment