在 Pyramid 中,如何根据上下文内容使用不同的渲染器? [英] In Pyramid, how can I use a different renderer based on contents of context?

查看:36
本文介绍了在 Pyramid 中,如何根据上下文内容使用不同的渲染器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据有关产品的可用信息显示 3 种不同的产品页面布局.使用遍历,我有一个名为 ProductFinder 的类,它可以获取所有信息.例如,用户转到 domain/green/small 并且 ProductFinder 将列出我的数据库中所有绿色和小型的产品.此列表是 ProductFinder 类中的 self.products.在我的 __init__.py 中,我添加了以下行:

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='')

在 products.py 我有:

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)

基于 context.products 中的内容,尽管我想呈现不同的 mako.在 Pylons 我会做这样的事情:

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?

推荐答案

我首先会说这种看起来像是您想要在模板中处理的事情.

I will start off by saying this sort of seems like something you want to take care of in your template.

然而,您可以以任何您想要的方式影响将哪个渲染器用作视图查找的一部分.您可能已经知道您可以对多个视图使用相同的视图处理程序,您只需要帮助 Pyramid 确定使用哪个.

However, you can influence which renderer is used as part of the view lookup in just about any way you want to. As you might already know you can use the same view handler for multiple views, you simply need to help Pyramid figure out which one to use.

例如:

from pyramid.view import view_config

def ProductLengthPredicate(length):
    def check_length(context, request):
        return len(context.products) == length
    return check_length

@view_config(context='app.models.ProductFinder', renderer='oneproduct.mako',
             custom_predicates=(ProductLengthPredicate(1),))
@view_config(context='app.models.ProductFinder', renderer='twoproducts.mako',
             custom_predicates=(ProductLengthPredicate(2),))
@view_config(context='app.models.ProductFinder', renderer='manyproducts.mako')
def products(context, request):
    return dict(page=context)

注意.有些人可能对 render_to_response 方法在这里,因为那样他们就不会依赖 custom_predicates.但这当然取决于你!

NB. Some people might be more interested in the render_to_response approach here because then they will not be relying on custom_predicates. But it is of course up to you!

@view_config(context='app.models.ProductFinder', renderer='manyproducts.mako')
def products(context, request)
    opts = dict(page=context)
    if len(context.products) == 1:
        return render_to_response('oneproduct.mako', opts, request)
    if len(context.products) == 2:
        return render_to_response('twoproducts.mako', opts, request)
    return opts

这是有效的,因为如果您的视图返回 Response() 这正是 render_to_response 所做的,Pyramid 将忽略渲染器.

This works because Pyramid will ignore the renderers if your view returns a Response() which is exactly what render_to_response does.

这篇关于在 Pyramid 中,如何根据上下文内容使用不同的渲染器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆