1 from django import template
2 from django.conf import settings
6 register = template.Library()
8 class View(pystache.View):
9 template_path = settings.TEMPLATE_DIRS[0]
11 def __init__(self, template_dir, template_name, context):
12 self.template_path = template_dir
13 self.template_name = template_name
14 return super(View, self).__init__(context=context)
16 class MustacheNode(template.Node):
17 def __init__(self, template_name, attr=None):
18 for template_dir in settings.TEMPLATE_DIRS:
19 if os.path.exists(os.path.join(template_dir, template_name) + ".mustache"):
22 raise IOError("failed to find %s in %s" % (template_name, str(settings.TEMPLATE_DIRS)))
24 self.template_path = template_dir
25 self.template = template_name
28 def render(self, context):
29 mcontext = context[self.attr] if self.attr else {}
30 view = View(self.template_path, self.template, context=mcontext)
33 def do_mustache(parser, token):
35 Loads a mustache template and render it inline
39 {% mustache "foo/bar" data %}
42 bits = token.split_contents()
43 if len(bits) not in [2,3]:
44 raise template.TemplateSyntaxError("%r tag takes two arguments: the location of the template file, and the template context" % bits[0])
48 return MustacheNode(path, *attrs)
51 register.tag("mustache", do_mustache)