[z3-five] Adapter and View insanity
Chris Withers
chris at simplistix.co.uk
Fri Nov 17 09:40:34 CET 2006
Martin Aspeli wrote:
> When you are passing context in like this, you may want to think about having
> multi-adapters that adapt the object being rendered and the context it's
> rendered in.
Nah, in this case, context could have easilly been "target" with the
adapted object being "source". I need the specific instance, so I see
where you're coming from, but no, it's not what I should do here...
>> <p tal:content="structure
>> python:modules['Products.MyProduct.interfaces'].IRender(item).render
> (context,title='Foo')"/>
>
> If the template was really a view, registered with <browser:page>, you'd have
> a class in which you cuold put this logic:
>
> class MyView(BrowserView):
>
> def render(self, item, title):
> return IRender(item).render(self.context, title)
>
> And then in the template:
>
> tal:replace="python:view.render(item, title)"
Ah yes, that is nice, but some of the places where it's used are not
conducive to being turned into views ;-)
So, I think I'll stick with @@render for now, no matter how much I bitch
about it ;-)
>> 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
>
> So you could instantiate once in a tal:define and put the logic in a method
> rather than in __call__().
Yuk.
>> 2. I can't pass in a title or explicit context. I have to work the
>> context out using acquisition
>
> Again solved by a method.
True enough, hopefully I won't need this...
>> 3. I'm deeply disturbed as to how instances of my Render view class come
>> to magically have a context attribute. wtf?
>
> It's a browser view. The ZCML directive injects a base class. Yes, it's nasty,
> which is why it's normally saner to explicitly inherit from
> Products.Five.browser.BrowserView.
See, I don't really understand what inheriting from BrowserView buys you...
Why is the magic there?!
> You can use restrictedTraverse() if you wish, but really you are doing:
>
> from zope.component import getMultiAdapter
> render = getMultiAdapter((context, view), name='render')
> print render()
What's 'view' in the above?
cheers,
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the z3-five
mailing list