Decorator: renders a template for a handler. The handler can control its behavior like that: - return a dict of template vars to fill out the template - return something other than a dict and the view decorator will not process the template, but return the h
(tpl_name, **defaults)
| 2791 | |
| 2792 | |
| 2793 | def view(tpl_name, **defaults): |
| 2794 | ''' Decorator: renders a template for a handler. |
| 2795 | The handler can control its behavior like that: |
| 2796 | |
| 2797 | - return a dict of template vars to fill out the template |
| 2798 | - return something other than a dict and the view decorator will not |
| 2799 | process the template, but return the handler result as is. |
| 2800 | This includes returning a HTTPResponse(dict) to get, |
| 2801 | for instance, JSON with autojson or other castfilters. |
| 2802 | ''' |
| 2803 | def decorator(func): |
| 2804 | @functools.wraps(func) |
| 2805 | def wrapper(*args, **kwargs): |
| 2806 | result = func(*args, **kwargs) |
| 2807 | if isinstance(result, (dict, DictMixin)): |
| 2808 | tplvars = defaults.copy() |
| 2809 | tplvars.update(result) |
| 2810 | return template(tpl_name, **tplvars) |
| 2811 | return result |
| 2812 | return wrapper |
| 2813 | return decorator |
| 2814 | |
| 2815 | mako_view = functools.partial(view, template_adapter=MakoTemplate) |
| 2816 | cheetah_view = functools.partial(view, template_adapter=CheetahTemplate) |