X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/b988d30ee8173f12ff54d7f0228239bacf4c723e..92e8e96c8d6814f3bd93ecd82d8da2ccb43e13c4:/templatetags/containers.py diff --git a/templatetags/containers.py b/templatetags/containers.py index ad9bb1d..c5fd445 100644 --- a/templatetags/containers.py +++ b/templatetags/containers.py @@ -13,41 +13,59 @@ class ContainerNode(template.Node): self.name = name self.as_var = as_var self.references = references + def render(self, context): content = settings.TEMPLATE_STRING_IF_INVALID if 'page' in context: - page = context['page'] - if self.references: - try: - contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references) - content = contentreference.content - except ObjectDoesNotExist: - pass - else: - try: - contentlet = page.contentlets.get(name__exact=self.name) - if contentlet.dynamic: - try: - content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context)) - except template.TemplateSyntaxError, error: - if settings.DEBUG: - content = ('[Error parsing contentlet \'%s\': %s]' % self.name, error) - else: - content = contentlet.content - except ObjectDoesNotExist: - pass - if content and self.as_var: - context[self.as_var] = content + container_content = self.get_container_content(context) + else: + container_content = None + + if self.as_var: + context[self.as_var] = container_content return '' + + if not container_content: + return '' + + return container_content + + def get_container_content(self, context): + page = context['page'] + if self.references: + # Then it's a content reference. + try: + contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references) + content = contentreference.content + except ObjectDoesNotExist: + content = '' + else: + # Otherwise it's a contentlet. + try: + contentlet = page.contentlets.get(name__exact=self.name) + if '{%' in contentlet.content or '{{' in contentlet.content: + try: + content = template.Template(contentlet.content, name=contentlet.name).render(context) + except template.TemplateSyntaxError, error: + if settings.DEBUG: + content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error)) + else: + content = settings.TEMPLATE_STRING_IF_INVALID + else: + content = contentlet.content + except ObjectDoesNotExist: + content = settings.TEMPLATE_STRING_IF_INVALID + content = mark_safe(content) return content def do_container(parser, token): """ - {% container [[references ] as ] %} + {% container [[references ] as ] %} """ params = token.split_contents() if len(params) >= 2: + tag = params[0] name = params[1].strip('"') references = None as_var = None @@ -60,19 +78,22 @@ def do_container(parser, token): app_label, model = remaining_tokens.pop(0).strip('"').split('.') references = ContentType.objects.get(app_label=app_label, model=model) except IndexError: - raise template.TemplateSyntaxError('"container" template tag option "references" requires an argument specifying a content type') + raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument specifying a content type' % tag) except ValueError: - raise template.TemplateSyntaxError('"container" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)') + raise template.TemplateSyntaxError('"%s" template tag option "references" requires an argument of the form app_label.model (see django.contrib.contenttypes)' % tag) except ObjectDoesNotExist: - 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)') + 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) elif option_token == 'as': try: as_var = remaining_tokens.pop(0) except IndexError: - raise template.TemplateSyntaxError('"container" template tag option "as" requires an argument specifying a variable name') + raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag) if references and not as_var: - raise template.TemplateSyntaxError('"container" template tags using "references" option require additional use of the "as" option specifying a variable name') + raise template.TemplateSyntaxError('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag) return ContainerNode(name, references, as_var) + else: # error - raise template.TemplateSyntaxError('"container" template tag provided without arguments (at least one required)') -register.tag('container', do_container) \ No newline at end of file + raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag) + + +register.tag('container', do_container)