3

In Pyramid, templates are statically assigned to view with

@view_config(renderer='templates/foo.pt')
def my_view(request):
    return {'foo':1, 'bar':2}

However, in some cases, we want to set different template for each user. For example, user1 use templates from templates/style1/xxx.pt and user2 use templates from templates/style2/xxx.pt. Thus, we need a way to dynamically pass templates to views, instead of configure them statically.

Is there anyway to do the task elegantly?

Lingfeng Xiong
  • 1,131
  • 2
  • 9
  • 25

2 Answers2

3

I just did this a couple of days ago.

Here is example code for an ajax call.

@view_config(name="my_view", renderer="")
def my_view(request):

    renderer = choose_renderer()            #this is where you would dynamically choose what renderer you want. ex "templates/foo.pt"
    data = get_my_data_for_renderer

    return render_to_response(renderer, data, request)

Here is an example for a normal route call

    @view_config(route_name="my_view", renderer="")
def my_view(request):

    renderer = choose_renderer()            #this is where you would dynamically choose what renderer you want. ex "templates/foo.pt"
    data = get_my_data_for_renderer

    return render_to_response(renderer, data, request)
fat fantasma
  • 7,483
  • 15
  • 48
  • 66
  • Thank you! Your solution gonna work, and it is very similar to http://stackoverflow.com/q/6553569/242786. However, it make the code redundant because every view must contain some lines to get the render and return with `render_to_response()`. I am looking for a elegant solution which can keep `@view_config` mostly unchanged, only make `renderer` to be a pattern or a relative path. – Lingfeng Xiong Aug 22 '15 at 20:16
  • I added a new answer that might help – fat fantasma Aug 22 '15 at 20:46
1

How about something like this. I have not tested it. I'm pulling from memory :)

Add this to your init.py in your config settings.

 config.add_route('my_view', 'my_view/{renderer}')

Use this view:

 @view_config(route_name='my_view', renderer='')
 def my_view(request):
    renderer = request.matchdict['renderer']
    data = get_my_data_for_renderer()
    return render_to_response(renderer, data, request)
fat fantasma
  • 7,483
  • 15
  • 48
  • 66
  • Yep, with this solution, every view still has redundant `renderer = request.matchdict['renderer']`. Currently I am digging into the implementation of `view_config` to see if I can set `renderer` as a pattern, like `renderer='template/{styledir}/main.tpl'` to get the task done. In this case, no more redundant code is needed in view logic. – Lingfeng Xiong Aug 22 '15 at 22:41
  • I'm not sure what to you mean by redundant code. Maybe you should put more examples what you want to do in your question. May even adding views dynamically at server start up could solve your problem. – fat fantasma Aug 23 '15 at 03:26