In Pyramid, how can I use a different renderer based on contents of context?

Viewed 3069

I have 3 different product page layouts that I would like to display dependent on the information available about the products. Using traversal I have a class called ProductFinder that grabs all the information. For example the user goes to domain/green/small and ProductFinder will list all products from my DB that are green and small. This list is self.products in the ProductFinder class. In my __init__.py I have added the line:

config.add_view('app.views.products', name='')

In products.py I have:

from pyramid.view import view_config
@view_config(context='app.models.ProductFinder', renderer='productpage.mako')
def products(context, request):
    return dict(page=context)

Based on what's in context.products though I'd like to render a different mako. In Pylons I would have done something like:

def products(context, request):
    if len(context.products) == 1:
        return render("oneproduct.mako")
    elif len(context.product) == 2:
        return render("twoproducts.mako")

So how can I render a different template based on the contents of my context?

2 Answers
Related