[Zope3-Users] Accessing adapter methods from zpt
James Allwyn
jamesallwyn at yahoo.co.uk
Wed Aug 17 04:46:17 EDT 2005
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
More information about the Zope3-users
mailing list