First, you want your Root resource to return different types of resources from __getitem__ :
class Root(object): def __getitem__(self, key): if key in ['Red', 'Green']: return ColorCategory(key) elif key in ['4', '5']: return SizeCategory(key) class ColorCategory(object): ... class SizeCategory(object): ...
Then in your __init__.py you want to configure different views for different types of resources (aka context types):
config.add_view('myapp.views.color', context='myapp:resources.ColorCategory', name='', renderer='myapp:templates/color.mak') config.add_view('myapp.views.size', context='myapp:resources.SizeCategory', name='', renderer='myapp:templates/size.mak')
The way this will work is that when you get a specific URL, Traversal will look for a specific context and view name. For domain.com/Red, the context will be ColorCategory('Red') (because it returns the Root resource), and the view name will be '' (because the path is completely absorbed after searching for this context). Then the pyramid will use the context type and view name as filters to search for the customized view and template.
source share