X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/ed946c07a3b070981477c2f41a76184678e13f4e..22c4e979b5ad061e448318310fddc4bb8fc56465:/templatetags/containers.py diff --git a/templatetags/containers.py b/templatetags/containers.py index 25156f2..8bb0c6b 100644 --- a/templatetags/containers.py +++ b/templatetags/containers.py @@ -9,79 +9,59 @@ register = template.Library() class ContainerNode(template.Node): - child_nodelists = ('nodelist_main', 'nodelist_empty',) - - def __init__(self, name, references=None, as_var=None, nodelist_main=None, nodelist_empty=None): + def __init__(self, name, references=None, as_var=None): self.name = name self.as_var = as_var self.references = references - - if nodelist_main is None: - self.nodelist_main = template.NodeList() - else: - self.nodelist_main = nodelist_main - - if nodelist_empty is None: - self.nodelist_empty = template.NodeList() - else: - self.nodelist_empty = nodelist_empty - + def render(self, context): content = settings.TEMPLATE_STRING_IF_INVALID if 'page' in context: - container_content = self.get_container_content(context['page']) - - if self.nodelist_main is None: - self.nodelist_main - if container_content and self.as_var: - context[self.as_var] = container_content - return '' - return container_content + container_content = self.get_container_content(context) + else: + container_content = None - if container_content: - if self.as_var is None: - self.as_var = self.name - - #make a new context - context.push() + if self.as_var: context[self.as_var] = container_content - nodelist = template.NodeList() - for node in self.nodelist_main: - nodelist.append(node.render(context)) - context.pop() - return nodelist.render(context) + return '' - if self.nodelist_empty is not None: - return self.nodelist_empty.render(context) + if not container_content: + return '' - return '' + return container_content - def get_container_content(self, page): + 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 contentlet.dynamic: + if '{%' in contentlet.content: try: - content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context)) + 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) + content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error)) + else: + content = settings.TEMPLATE_STRING_IF_INVALID else: content = contentlet.content except ObjectDoesNotExist: - content = '' + content = settings.TEMPLATE_STRING_IF_INVALID + content = mark_safe(content) return content + def do_container(parser, token): """ - {% container [[references ] as ] %} - {% blockcontainer [[references ] as ] %} [ {% empty %} ] {% endblockcontainer %} + {% container [[references ] as ] %} """ params = token.split_contents() if len(params) >= 2: @@ -110,20 +90,10 @@ def do_container(parser, token): 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('"%s" template tags using "references" option require additional use of the "as" option specifying a variable name' % tag) - if tag == 'container': - return ContainerNode(name, references, as_var) - - nodelist_main = parser.parse(('empty','endblockcontainer',)) - token = parser.next_token() - - if token.contents == 'empty': - nodelist_empty = parser.parse(('endblockcontainer',)) - parser.delete_first_token() - else: - nodelist_empty = None - return ContainerNode(name, references, as_var, nodelist_main, nodelist_empty) + return ContainerNode(name, references, as_var) else: # error raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag) + + register.tag('container', do_container) -register.tag('blockcontainer', do_container)