from django import template from middleware import threadlocals register = template.Library() @register.tag def embed(parser, token): try: tag_name, id, template = token.split_contents() except ValueError: raise template.TemplateSyntaxError, "%r tag requires a element id and a template path" % token.contents.split()[0] nodelist = parser.parse(('endembed',)) parser.delete_first_token() return Embed(nodelist, id, template) class Embed(template.Node): def __init__(self, nodelist, id, template): self.nodelist = nodelist self.id = id self.template = template def render(self, context): try: value = self.nodelist.render(context) return template.loader.render_to_string(self.template, { "id": self.id, "template": value, }) except template.TemplateDoesNotExist: raise template.TemplateDoesNotExist, "Template does not exist: %s" % self.template