Views
Posted by Graham Stratton Fri, 12 May 2006 09:26:00 GMT
One thing that got me for a while with Zope3 is how view lookups work. A view is looked up by the object, the request and the view name. What does that mean? Surely the object comes from the request anyway?
Yes, but the lookup is done by interfaces. So the object is likely to provide IMyObject, the request IBrowserRequest, and the view name something like index.html. When you defined your object, your will probably have also defined a browser view for index.html, and this is what will be found.
A view for ”*” will be registered for Interface, so the lookup will match any object type, since all interfaces inherit from Interface. So interface lookup climbs the inheritance tree. I don’t know how it deals with multiple inheritance, though. Probably in the same way as Python (which changed in version 2.2).
So what is a view? Well, it’s a class which adapts an object and a request. That means that what you instantiate the class, you pass in an object and a request. Okay, but then what? Well, when you call that instance, it should return the HTML for that view.
Okay, that’s that sorted then. Let’s more on to content providers. A content provider adapts an object, a request and a view to IContentProvider. IContentProvider requires that the object have attributes request, context (the object), and _parent_ (the view). It should also provide an update() method, and a render() method, which returns the HTML.
So, a content provider lookup discriminates against the view. What does that do for us? Well, we might have a content provider contains the CSS loading code. We might want a different stylesheet for our management and non-management screens. So if all our views either implement IMyManagementView or IMyNonManagementView, then by looking up a content provider called, say cssimport, then we could get the appropriate one for our view, assuming that we had registered content providers for both Interfaces.
