Added directives and autodocumenters for template tags and filters in a custom extens...
[philo.git] / philo / templatetags / include_string.py
1 from django import template
2 from django.conf import settings
3
4
5 register = template.Library()
6
7
8 class IncludeStringNode(template.Node):
9         def __init__(self, string):
10                 self.string = string
11         
12         def render(self, context):
13                 try:
14                         t = template.Template(self.string.resolve(context))
15                         return t.render(context)
16                 except template.TemplateSyntaxError:
17                         if settings.TEMPLATE_DEBUG:
18                                 raise
19                         return settings.TEMPLATE_STRING_IF_INVALID
20                 except:
21                         return settings.TEMPLATE_STRING_IF_INVALID
22
23
24 @register.tag
25 def include_string(parser, token):
26         """
27         Include a flat string by interpreting it as a template. The compiled template will be rendered with the current context.
28         
29         Usage::
30         
31                 {% include_string <template_code> %}
32         
33         """
34         bits = token.split_contents()
35         if len(bits) != 2:
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)