[Zope3-Users] Accessing adapter methods from zpt

Roger Ineichen dev at projekt01.ch
Wed Aug 17 13:19:28 EDT 2005


Hi Mike and James

> -----Original Message-----
> From: zope3-users-bounces at zope.org 
> [mailto:zope3-users-bounces at zope.org] On Behalf Of Michael 
> van Slingerland
> Sent: Wednesday, August 17, 2005 6:33 PM
> To: James Allwyn
> Cc: zope3-users at zope.org
> Subject: Re: [Zope3-Users] Accessing adapter methods from zpt
> 
> Hi James,
> 
> Thanks for your thorough explanation with examples, good stuff!
> 
> I had the feeling that views needed to be used but it looked 
> so inefficient.
> Is there a thought/philosophy behind the fact that we can't access
> adapters directly from ZPT?

Did you take a look at the pagelet implementation?

See:
src\zope\app\demo\pagelet\browser\demo_pagedata_pagelet.pt
there is something what are you looking for. 

I implemented a ITALESExpression where you can use in ZPT's like:

<tal:block tal:define="data
pagedata:zope.app.demo.pagelet.interfaces.IDemoPageData">
  <span tal:content="data/title">title</span>
</tal:block>

The adapter can you register like:
<!-- pagedata adapter -->
<zope:adapter
    for="zope.interface.Interface
         zope.publisher.interfaces.browser.IBrowserRequest
         zope.component.interfaces.IView"
    factory=".views.DemoPageData"
    provides="..interfaces.IDemoPageData"
    />

Perhas I should implement a directive for this like:
<pagedata
	for="zope.interface.Interface"
      layer="zope.publisher.interfaces.browser.IBrowserRequest"
	view="zope.component.interfaces.IView"
	factory=".views.DemoPageData"
	provides="..interfaces.IDemoPageData"
	/>

Which is more usable and userfriendly since it explains the attributes.
But remember the factory has to provide a constructor like:

  class DemoPageData(context, request, view):

and this is perhaps not usefull in your usecase. 

I wrote the pagedata directive just for call such adapters independent 
from a view. This means you can access this adapter from every ZPT.

But there are simpler concept like to register a method as a view.
Such a method can be called from every ZPT too. 
See context/@@absolute_url as a example.


Regards
Roger Ineichen

Projekt01 GmbH
www.projekt01.ch
_____________________________
END OF MESSAGE  

> Thanks,
> Mike
> 
> > Mike,
> >
> > When I started getting into adapters recently I
> > discovered that you can't (easily) access adapters
> > directly from ZPT. You will need to create a python
> > view class as an intermediate step.
> >
> > Where you use a "context" statement to access
> > properties of an object in ZPT, e.g.:
> >
> > <div class="field" tal:content="context/description"
> > />
> >
> > you use a "view" statement to get at the methods of
> > the Python View Class, e.g.:
> >
> > <div class="field" tal:content="view/rating" />
> >
> > You use the ZCML to wire up the ZPT to the Python,
> > e.g.:
> >
> >   <page
> >       name="someview.html"
> >       for="dotted.path.to.IObject"
> >       class=".pythonfile.ViewClass"
> >       template="template.pt"
> >       permission="zope.Public"
> >       menu="zmi_views" title="View"/>
> >
> > Where python file is the file the viewclass is in,
> > usually in the same browser folder, and ViewClass is
> > the name of a class defined within that file.
> >
> > The ViewClass itself can do whatever logic you want to
> > prepare data for the ZPT, but if it's just grabbing
> > adapted methods and re-presenting them, it'll be
> > something like this:
> >
> > from wherever import IAdapter
> >
> > class ViewClass:
> >
> >     def __init__ (self, context, request):
> >         self.context = context
> >         self.request = request
> >
> >         self.adaptation = IAdapter(context)
> >
> >     def method(self):
> >         """Get the adapted doodle from the adapter"""
> >         adapted = self.adaptation.adaptermeth()
> >         return adapted
> >
> > Where "adaptermeth" is the method defined in your
> > adapter, and "IAdapter" is the name of your adapter.
> > You probably won't want to reference it as
> > self.adaptation - call if something more meaningful -
> > self.myimagefunction, or whatever.
> >
> > You will need to def a method for each adapted method
> > you want to grab from adapters and pass to the ZPT.
> >
> > If you have more than one adapter you want to access
> > from a given ZPT you need to make sure you import them
> > all, and use a different "self.adapterdesc =" in the
> > __init__, and you can then call each from the methods
> > of the view class.
> >
> > Hope that helps,
> > James
> >
> > --- Michael van Slingerland <z3uml at xs4all.nl> wrote:
> >
> >> Hi all,
> >>
> >> I've made an adapter on the image content type and
> >> built a new view on the
> >> IImage interface. But what I can't find anywhere is
> >> how to access methods
> >> defined in the adapter from ZPT?
> >>
> >> Anyone knows howto do this?
> >>
> >> Thanks,
> >> Mike
> >>
> >> _______________________________________________
> >> Zope3-users mailing list
> >> Zope3-users at zope.org
> >> http://mail.zope.org/mailman/listinfo/zope3-users
> >>
> >
> >
> >
> >
> > ___________________________________________________________
> > To help you stay safe and secure online, we've developed the all new
> > Yahoo! Security Centre. http://uk.security.yahoo.com
> > _______________________________________________
> > Zope3-users mailing list
> > Zope3-users at zope.org
> > http://mail.zope.org/mailman/listinfo/zope3-users
> >
> 
> 
> _______________________________________________
> Zope3-users mailing list
> Zope3-users at zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users
> 



More information about the Zope3-users mailing list