2 .. templatetag:: include_string
7 Include a flat string by interpreting it as a template. The compiled template will be rendered with the current context.
11 {% include_string <template_code> %}
15 from django import template
16 from django.conf import settings
19 register = template.Library()
22 class IncludeStringNode(template.Node):
23 def __init__(self, string):
26 def render(self, context):
28 t = template.Template(self.string.resolve(context))
29 return t.render(context)
30 except template.TemplateSyntaxError:
31 if settings.TEMPLATE_DEBUG:
33 return settings.TEMPLATE_STRING_IF_INVALID
35 return settings.TEMPLATE_STRING_IF_INVALID
38 def do_include_string(parser, token):
39 bits = token.split_contents()
41 raise TemplateSyntaxError("%r tag takes one argument: the template string to be included" % bits[0])
42 string = parser.compile_filter(bits[1])
43 return IncludeStringNode(string)
46 register.tag('include_string', do_include_string)