X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/ea27ab0095dece2f0ffcfbb570c462b3b4e6e993..7ea527579abe3945bc5c17063fb94387a3cc981f:/templatetags/containers.py diff --git a/templatetags/containers.py b/templatetags/containers.py index 45a9965..2e40e53 100644 --- a/templatetags/containers.py +++ b/templatetags/containers.py @@ -9,19 +9,30 @@ 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): self.name = name self.as_var = as_var self.references = references - self.nodelist_main = nodelist_main - self.nodelist_empty = nodelist_empty + 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']) + container_content = self.get_container_content(context) if self.nodelist_main is None: + self.nodelist_main if container_content and self.as_var: context[self.as_var] = container_content return '' @@ -30,16 +41,23 @@ class ContainerNode(template.Node): if container_content: if self.as_var is None: self.as_var = self.name + + #make a new context context.push() context[self.as_var] = container_content - return nodelist_main.render(context) + nodelist = template.NodeList() + for node in self.nodelist_main: + nodelist.append(node.render(context)) + context.pop() + return nodelist.render(context) - if nodelist_empty is not None: - return nodelist_empty.render(context) + if self.nodelist_empty is not None: + return self.nodelist_empty.render(context) return '' - def get_container_content(self, page): + def get_container_content(self, context): + page = context['page'] if self.references: try: contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references) @@ -54,13 +72,14 @@ class ContainerNode(template.Node): 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) + content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error)) else: content = contentlet.content except ObjectDoesNotExist: content = '' return content + def do_container(parser, token): """ {% container [[references ] as ] %} @@ -104,8 +123,11 @@ def do_container(parser, token): parser.delete_first_token() else: nodelist_empty = None - return BlockContainerNode(name, references, as_var, nodelist_main, nodelist_empty) + return ContainerNode(name, references, as_var, nodelist_main, nodelist_empty) else: # error raise template.TemplateSyntaxError('"%s" template tag provided without arguments (at least one required)' % tag) -register.tag('container', do_container) \ No newline at end of file + + +register.tag('container', do_container) +register.tag('blockcontainer', do_container)