from django import template from middleware import threadlocals regist

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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