传统方式似乎很好:
@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引擎自行渲染一个仲裁模板并将其作为回复返回?
答案 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)