如何在金字塔中精确控制视图渲染

时间:2013-11-06 19:41:18

标签: pyramid mako

传统方式似乎很好:

@view_config(route_name='new', renderer='new.mako')
    return {'tasks': tasks}

但有时我可能需要对我正在呈现的内容进行精细控制,即我可能会根据条件呈现不同的观点。请参阅此伪代码:

@view_config(route_name='new')
def new_view(request):
    if request.att == something:
        one_dict = ...
        a = render( "new.mako", one_dict)
    else:
        another_dict = ...
        a = render( "new_special.mako", one_dict)
    return a

如何使用Pyramid的Mako引擎自行渲染一个仲裁模板并将其作为回复返回?

1 个答案:

答案 0 :(得分:1)

您可以在此处使用render_to_response() renderer

  

使用渲染器renderer_name(模板或静态渲染器),使用渲染器的__call__方法(通常是字符串或Unicode)的结果渲染值(或值集)回应机构。

对于您的代码:

@view_config(route_name='new')
def new_view(request):
    if request.att == something:
        one_dict = ...
        a = render_to_response("new.mako", one_dict)
    else:
        another_dict = ...
        a = render_to_response("new_special.mako", one_dict)
    return a

或者也许:

@view_config(route_name='new')
def new_view(request):
    if request.att == something:
        renderer = "new.mako"
    else:
        renderer = "new_special.mako"
    return render_to_response(renderer, values)