1 from django import template
2 from django.conf import settings
5 register = template.Library()
8 class IncludeStringNode(template.Node):
9 def __init__(self, string):
12 def render(self, context):
14 t = template.Template(self.string.resolve(context))
15 return t.render(context)
16 except template.TemplateSyntaxError:
17 if settings.TEMPLATE_DEBUG:
19 return settings.TEMPLATE_STRING_IF_INVALID
21 return settings.TEMPLATE_STRING_IF_INVALID
25 def include_string(parser, token):
27 Include a flat string by interpreting it as a template. The compiled template will be rendered with the current context.
31 {% include_string <template_code> %}
34 bits = token.split_contents()
36 raise TemplateSyntaxError("%r tag takes one argument: the template string to be included" % bits[0])
37 string = parser.compile_filter(bits[1])
38 return IncludeStringNode(string)