2 The container template tags are automatically included as builtins if :mod:`philo` is an installed app.
6 from django import template
7 from django.conf import settings
8 from django.contrib.contenttypes.models import ContentType
9 from django.core.exceptions import ObjectDoesNotExist
10 from django.utils.safestring import SafeUnicode, mark_safe
13 register = template.Library()
16 class ContainerNode(template.Node):
17 def __init__(self, name, references=None, as_var=None):
20 self.references = references
22 def render(self, context):
23 content = settings.TEMPLATE_STRING_IF_INVALID
25 container_content = self.get_container_content(context)
27 container_content = None
30 context[self.as_var] = container_content
33 if not container_content:
36 return container_content
38 def get_container_content(self, context):
39 page = context['page']
41 # Then it's a content reference.
43 contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
44 content = contentreference.content
45 except ObjectDoesNotExist:
48 # Otherwise it's a contentlet.
50 contentlet = page.contentlets.get(name__exact=self.name)
51 if '{%' in contentlet.content or '{{' in contentlet.content:
53 content = template.Template(contentlet.content, name=contentlet.name).render(context)
54 except template.TemplateSyntaxError, error:
56 content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error))
58 content = settings.TEMPLATE_STRING_IF_INVALID
60 content = contentlet.content
61 except ObjectDoesNotExist:
62 content = settings.TEMPLATE_STRING_IF_INVALID
63 content = mark_safe(content)
68 def container(parser, token):
70 If a template using this tag is used to render a :class:`.Page`, that :class:`.Page` will have associated content which can be set in the admin interface. If a content type is referenced, then a :class:`.ContentReference` object will be created; otherwise, a :class:`.Contentlet` object will be created.
74 {% container <name> [[references <app_label>.<model_name>] as <variable>] %}
77 params = token.split_contents()
80 name = params[1].strip('"')
84 remaining_tokens = params[2:]
85 while remaining_tokens:
86 option_token = remaining_tokens.pop(0)
87 if option_token == 'references':
89 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
90 references = ContentType.objects.get_by_natural_key(app_label, model)
92 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
94 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
95 except ObjectDoesNotExist:
96 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model which refers to an installed content type (see django.contrib.contenttypes)' % tag)
97 elif option_token == 'as':
99 as_var = remaining_tokens.pop(0)
101 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
102 if references and not as_var:
103 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
104 return ContainerNode(name, references, as_var)
107 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)