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 content = contentlet.content
52 except ObjectDoesNotExist:
58 def container(parser, token):
60 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.
64 {% container <name> [[references <app_label>.<model_name>] as <variable>] %}
67 params = token.split_contents()
70 name = params[1].strip('"')
74 remaining_tokens = params[2:]
75 while remaining_tokens:
76 option_token = remaining_tokens.pop(0)
77 if option_token == 'references':
79 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
80 references = ContentType.objects.get_by_natural_key(app_label, model)
82 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
84 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
85 except ObjectDoesNotExist:
86 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)
87 elif option_token == 'as':
89 as_var = remaining_tokens.pop(0)
91 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
92 if references and not as_var:
93 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
94 return ContainerNode(name, references, as_var)
97 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)