1 from django import template
2 from django.template.loader_tags import do_include
3 from myslice.settings import theme
5 register = template.Library()
7 class IncludeNode(template.Node):
9 def __init__(self, template_name):
11 self.theme_template_name = "%s%s" % (theme, template_name)
12 self.template_name = template_name
14 def render(self, context):
16 # Loading the template and rendering it
17 included_template = template.loader.get_template(self.theme_template_name).render(context)
18 except template.TemplateDoesNotExist:
19 # template theme does not exists, try the generic one
21 # Loading the template and rendering it
22 included_template = template.loader.get_template(self.template_name).render(context)
23 except template.TemplateDoesNotExist:
24 included_template = ''
26 return included_template
29 def widget(parser, token):
30 """Usage: {% widget "widget.html" %}
32 This will fail silently if the template doesn't exist. If it does, it will
33 be rendered with the current context."""
35 tag_name, template_name = token.split_contents()
37 raise template.TemplateSyntaxError, \
38 "%r tag requires a single argument" % token.contents.split()[0]
40 return IncludeNode(template_name[1:-1])