Selecting a render method from a view using the URL

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.

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

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

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.

1 Like

Here's another working example, that given a url /@@stream/tag/foo extracts the "foo" tag argument:

2 Likes