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.db.models import Q
11 from django.utils.safestring import SafeUnicode, mark_safe
14 register = template.Library()
17 CONTAINER_CONTEXT_KEY = 'philo_container_context'
20 class ContainerContext(object):
21 def __init__(self, contentlets, references):
22 self.contentlets = dict(((c.name, c) for c in contentlets))
23 self.references = dict((((c.name, ContentType.objects.get_for_id(c.content_type_id)), c) for c in references))
26 class ContainerNode(template.Node):
27 def __init__(self, name, references=None, as_var=None):
30 self.references = references
32 def render(self, context):
33 container_content = self.get_container_content(context)
36 context[self.as_var] = container_content
39 return container_content
41 def get_container_content(self, context):
43 container_context = context.render_context[CONTAINER_CONTEXT_KEY]
46 page = context['page']
48 return settings.TEMPLATE_STRING_IF_INVALID
50 contentlets = page.contentlets.all()
51 references = page.contentreferences.all()
53 container_context = ContainerContext(contentlets, references)
54 context.render_context[CONTAINER_CONTEXT_KEY] = container_context
57 # Then it's a content reference.
59 contentreference = container_context.references[(self.name, self.references)]
63 content = contentreference.content
65 # Otherwise it's a contentlet.
67 contentlet = container_context.contentlets[self.name]
71 content = contentlet.content
76 def container(parser, token):
78 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.
82 {% container <name> [[references <app_label>.<model_name>] as <variable>] %}
85 params = token.split_contents()
88 name = params[1].strip('"')
92 remaining_tokens = params[2:]
93 while remaining_tokens:
94 option_token = remaining_tokens.pop(0)
95 if option_token == 'references':
97 app_label, model = remaining_tokens.pop(0).strip('"').split('.')
98 references = ContentType.objects.get_by_natural_key(app_label, model)
100 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag)
102 raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag)
103 except ObjectDoesNotExist:
104 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)
105 elif option_token == 'as':
107 as_var = remaining_tokens.pop(0)
109 raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
110 if references and not as_var:
111 raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag)
112 return ContainerNode(name, references, as_var)
115 raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag)