Plone: traverse_subpath for BrowserViews: @@view/some/more/path

Zope BrowserView: parameter-like subpaths
Push Button

Sometimes you dont want GET-parameters, but a subpath. In Plone its very easy to achieve this.




 This topic is documented somehow out there, but - sorry - in a way that I didnt get it.


To do so we need

from Products.Five.browser import BrowserView
from zope.interface import implementer
from zope.publisher.interfaces import IPublishTraverse
and then create a normal view-class like so
class MyTraversingView(BrowserView):

    def __call__(self):        
        return self.subpath

    def publishTraverse(self, request, name):
        if not hasattr(self, 'subpath'):
            self.subpath = []
        return self

Also we need some configure.zcml like so


So what happens? Instead of ending the traversal on the view the IPublishTraverse interface is found by the publisher. Now it looks for a publishTraverse method and calls it until the traversal stack is empty.

photo by Thimothy Allan (Thristan) at flickr under CC-BY-SA License