alberto
(Alberto Lopes)
September 21, 2016, 9:47pm
1
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?
Thanks.
espenmn
(Espen)
September 21, 2016, 10:22pm
2
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:
http://site/test?view=alpha
jensens
(Jens W. Klein)
September 21, 2016, 10:27pm
3
I wrote this blog post addressing exactly what you need first: https://bluedynamics.com/blog/jens/plone-traverse_subpath-for-browserviews-view-some-more-path
After you got the name its easy to conditionally render one or the other template.
1 Like
petri
(Petri Savolainen)
September 23, 2016, 6:29am
4
Doesn't that just build another copy of what's already in request['TraversalRequestNameStack']?
jensens
(Jens W. Klein)
September 23, 2016, 6:45am
5
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.
1 Like
gyst
(Guido Stevens)
September 23, 2016, 7:06am
6
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
def update(self):
'''Mute plone.app.z3cform.kss.validation AttributeError'''
pass
def publishTraverse(self, request, name):
''' used for traversal via publisher, i.e. when using as a url '''
if name == 'tag':
stack = request.get('TraversalRequestNameStack')
try:
self.tag = stack.pop()
except IndexError:
# don't traceback on missing tag spec
self.tag = None
elif name == 'network':
# @@stream/network enables 'following' filter
2 Likes