Suppose I have a "test" view, registered for "*" in configure.zcml. The view has a TestView class.
I have two templates, alpha.pt and beta.pt. I want the view to be able to select the template based on the request URL, like so:
My question is about getting the last part of the URL as a parameter to the
call method of the view. How can I do that?
Why not just register two views with the same class but with different templates ?
Or maybe you could define which template to use in the view.py and call it like:
I wrote this blog post addressing exactly what you need first:
After you got the name its easy to conditionally render one or the other template.
Doesn't that just build another copy of what's already in request['TraversalRequestNameStack']?
In fact it more or less does. But at every step one may not return self, but a different object - even one out of ZODB. And instead of building a list one may want to build a dict or what you like out of it as well. And it does this at traversal time. So many additional options.
Here's another working example, that given a url
/@@stream/tag/foo extracts the "foo" tag argument:
self.context = context
self.request = request
self.tag = ''
# default to full stream
self.explore = True
'''Mute plone.app.z3cform.kss.validation AttributeError'''
def publishTraverse(self, request, name):
''' used for traversal via publisher, i.e. when using as a url '''
if name == 'tag':
stack = request.get('TraversalRequestNameStack')
self.tag = stack.pop()
# don't traceback on missing tag spec
self.tag = None
elif name == 'network':
# @@stream/network enables 'following' filter