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.

 

 http://www.mysite.tld/folder/page/viewname/some/other/value

 

 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
@implementer(IPublishTraverse)
class MyTraversingView(BrowserView):

    def __call__(self):        
        return self.subpath

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

Also we need some configure.zcml like so

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser">
    <browser:page
        for="*"
        name="mytraversingview"
        class=".views.MyTraversingView"
        permission="zope2.View"
    />
</configure>

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