1 from django import template
2 from django.conf import settings
3 from django.utils.safestring import SafeUnicode, mark_safe
4 from django.core.exceptions import ObjectDoesNotExist
5 from django.contrib.contenttypes.models import ContentType
8 register = template.Library()
11 class ContainerNode(template.Node):
12 def __init__(self, name, references=None, as_var=None):
15 self.references = references
16 def render(self, context):
17 content = settings.TEMPLATE_STRING_IF_INVALID
19 page = context['page']
22 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
23 content = contentreference.content
24 except ObjectDoesNotExist:
28 contentlet = page.contentlets.get(name__exact=self.name)
29 if contentlet.dynamic:
31 content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context))
32 except template.TemplateSyntaxError, error:
34 content = ('[Error parsing contentlet \'%s\': %s]' % self.name, error)
36 content = contentlet.content
37 except ObjectDoesNotExist:
39 if content and self.as_var:
40 context[self.as_var] = content
45 def do_container(parser, token):
47 {% container <name> [[references <type>] as <variable>] %}
49 params = token.split_contents()
51 name = params[1].strip('"')
55 remaining_tokens = params[2:]
56 while remaining_tokens:
57 option_token = remaining_tokens.pop(0)
58 if option_token == 'references':
60 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
61 references = ContentType.objects.get(app_label=app_label, model=model)
63 raise template.TemplateSyntaxError('"container" template tag option "references" requires an argument specifying a content type')
65 raise template.TemplateSyntaxError('"container" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)')
66 except ObjectDoesNotExist:
67 raise template.TemplateSyntaxError('"container" template tag option "references" requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)')
68 elif option_token == 'as':
70 as_var = remaining_tokens.pop(0)
72 raise template.TemplateSyntaxError('"container" template tag option "as" requires an argument specifying a variable name')
73 if references and not as_var:
74 raise template.TemplateSyntaxError('"container" template tags using "references" option require additional use of the "as" option specifying a variable name')
75 return ContainerNode(name, references, as_var)
77 raise template.TemplateSyntaxError('"container" template tag provided without arguments (at least one required)')
78 register.tag('container', do_container)