Skip to content

Instantly share code, notes, and snippets.

@bohde
Created December 20, 2011 23:09
Show Gist options
  • Select an option

  • Save bohde/1503729 to your computer and use it in GitHub Desktop.

Select an option

Save bohde/1503729 to your computer and use it in GitHub Desktop.
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