[z3-five] Adapter and View insanity

Chris Withers chris at simplistix.co.uk
Thu Nov 16 12:10:32 CET 2006


Hi All,

I need to render representations of objects and sometimes override their 
titles, so I came up with an interface:

class IRender(Interface):

     def render(context,title=None)
       ""

The context parameter is because the rendering can change depending on 
where its being used.

So, this works really well from code and I end up with nice little 
snippets like:

self._html = IRender(obj).render(context,title)

Okay, so now I want to re-use this to render objects inside a ZPT.

My first attempt at this was horrific:

<p tal:content="structure 
python:modules['Products.MyProduct.interfaces'].IRender(item).render(context,title='Foo')"/>

Having cleaned up the resulting vomit, I decided to try and use a view, 
as suggested by Martin A. The only way I could seem to do that was to 
have a whole seperate class:

class Render:

     def __call__(self):
         return IRender(self.context).render(self.context)

...which I then have to write zcml to register:

   <browser:page
       for="*"
       name="render"
       class=".views.Render"
       permission="zope2.View"/>

Which means I can finally do this:

<p tal:content="structure item/@@render"/>

But there's still a few problems:

1. I'm instantiating yet another class every time I just want to adapt
    something and call a method

2. I can't pass in a title or explicit context. I have to work the
    context out using acquisition :-(

3. I'm deeply disturbed as to how instances of my Render view class come
    to magically have a context attribute. wtf?

So, anyway I'm left wondering:

- what's the _minimum_ I need to do to be able to use something like:

<p tal:content="structure item/@@render"/>

...to mean "adapt item to IRender and call its render method"?

- how do I do the equivalent of item/@@render in python? (I'm hoping 
this will enlighten me as to how I'd pass an explicit context and title in)

cheers,

Chris

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


More information about the z3-five mailing list