[Zope-dev] zpt nesting/inheritence in views

Chris Withers chris at simplistix.co.uk
Fri Apr 17 21:36:24 EDT 2009


Hey All,

Thought I'd share this and see if anyone had any better ideas...

So, I have a set of views that subclass each other, nothing out of the 
ordinary there. However, I also want the zpts used by the views to 
"subclass" each other, such that a subclassing view can use its parent's 
macro(s) and provide the same to be used further downstream.

This is the only solution I can think of:

from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

class Base1(BrowserView):
     template = ViewPageTemplateFile('html/template1.html')

class Base2(Base1):
     template = ViewPageTemplateFile('html/template2.html')
     parent = Base1

class Base3(Base2):
     template = ViewPageTemplateFile('html/template3.html')
     parent = Base2

class Concrete(Base3):
     def __call__(self):
         return self.template(self)

template1.html:

<metal:x define-macro="main">
   <p>Template1</p>
   <metal:x define-slot="slot"/>
</metal:x>

template2.html:

<tal:x define="parent nocall:view">
<metal:x define-macro="main"
          tal:define="parent nocall:parent/parent">
<metal:x use-macro="parent/template/macros/main">
   <metal:x fill-slot="slot">
   <p>Template2</p>
   <metal:x define-slot="slot"/>
   </metal:x>
</metal:x>
</metal:x>
</tal:x>

template3.html:

<tal:x define="parent nocall:view">
<tal:x define="parent nocall:parent/parent">
<metal:x use-macro="parent/template/macros/main">
   <metal:x fill-slot="slot">
   <p>Template3</p>
   </metal:x>
</metal:x>
</tal:x>
</tal:x>

Can anyone think of a nicer way that doesn't involve the 'parent' 
attribute on the view classes and has (preferably much) nicer code in 
the zpts?

cheers,

Chris

-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope-Dev mailing list