Moved templatetag docs into the respective modules and fleshed them out. Centralized...
[philo.git] / philo / templatetags / include_string.py
1 """
2 .. templatetag:: include_string
3
4 include_string
5 --------------
6
7 Include a flat string by interpreting it as a template. The compiled template will be rendered with the current context.
8
9 Usage::
10
11         {% include_string <template_code> %}
12
13 """
14
15 from django import template
16 from django.conf import settings
17
18
19 register = template.Library()
20
21
22 class IncludeStringNode(template.Node):
23         def __init__(self, string):
24                 self.string = string
25         
26         def render(self, context):
27                 try:
28                         t = template.Template(self.string.resolve(context))
29                         return t.render(context)
30                 except template.TemplateSyntaxError:
31                         if settings.TEMPLATE_DEBUG:
32                                 raise
33                         return settings.TEMPLATE_STRING_IF_INVALID
34                 except:
35                         return settings.TEMPLATE_STRING_IF_INVALID
36
37
38 def do_include_string(parser, token):
39         bits = token.split_contents()
40         if len(bits) != 2:
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)
44
45
46 register.tag('include_string', do_include_string)