Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File buffet_patch.diff,
4.9 KB
(added by chrisz, 2 years ago) |
|
Support for options in render method.
|
-
|
|
|
|
| 34 | 34 | |
| 35 | 35 | |
| 36 | 36 | def _process_output(output, template, format, content_type, |
| 37 | | mapping, fragment=False): |
| | 37 | fragment=False, **options): |
| 38 | 38 | """Produce final output form from data returned from a controller method. |
| 39 | 39 | |
| 40 | 40 | See the expose() arguments for more info since they are the same. |
| … |
… |
|
| 92 | 92 | |
| 93 | 93 | headers = {'Content-Type': content_type} |
| 94 | 94 | output = view.render(output, template=template, format=format, |
| 95 | | mapping=mapping, headers=headers, |
| 96 | | fragment=fragment) |
| | 95 | headers=headers, fragment=fragment, **options) |
| 97 | 96 | content_type = headers['Content-Type'] |
| 98 | 97 | |
| 99 | 98 | if content_type: |
| … |
… |
|
| 260 | 259 | |
| 261 | 260 | def expose(template=None, allow_json=None, |
| 262 | 261 | format=None, content_type=None, fragment=False, |
| 263 | | as_format="default", mapping=None, accept_format=None): |
| | 262 | as_format="default", accept_format=None, **options): |
| 264 | 263 | """Exposes a method to the web. |
| 265 | 264 | |
| 266 | 265 | By putting the expose decorator on a method, you tell TurboGears that |
| … |
… |
|
| 314 | 313 | just generate the immediate template fragment. Use this |
| 315 | 314 | if you're building up a page from multiple templates or |
| 316 | 315 | going to put something onto a page with .innerHTML. |
| 317 | | @keyparam mapping mapping with options that are sent to the template |
| 318 | | engine |
| 319 | 316 | @keyparam as_format designates which value of tg_format will choose |
| 320 | 317 | this expose. |
| 321 | 318 | @keyparam accept_format which value of an Accept: header will |
| 322 | 319 | choose this expose. |
| 323 | 320 | |
| | 321 | All additional options are passed as keyword parameters |
| | 322 | to the render method of the template engine. |
| | 323 | |
| 324 | 324 | """ |
| 325 | 325 | if not template: |
| 326 | 326 | template = format |
| … |
… |
|
| 384 | 384 | accept_format=accept_format, template=template, |
| 385 | 385 | rulefunc=lambda _func, accept, allow_json, *args, **kw: |
| 386 | 386 | _execute_func(_func, template, format, content_type, |
| 387 | | mapping, fragment, args, kw))) |
| | 387 | fragment, options, args, kw))) |
| 388 | 388 | |
| 389 | 389 | if allow_json: |
| 390 | 390 | func._allow_json = True |
| … |
… |
|
| 393 | 393 | return weak_signature_decorator(entangle) |
| 394 | 394 | |
| 395 | 395 | |
| 396 | | def _execute_func(func, template, format, content_type, mapping, fragment, args, kw): |
| | 396 | def _execute_func(func, template, format, content_type, fragment, |
| | 397 | options, args, kw): |
| 397 | 398 | """Call controller method and process it's output.""" |
| 398 | 399 | |
| 399 | 400 | if config.get("tg.strict_parameters", False): |
| … |
… |
|
| 440 | 441 | format = output.pop("tg_format", format) |
| 441 | 442 | |
| 442 | 443 | if template and template.startswith("."): |
| 443 | | template = func.__module__[:func.__module__.rfind('.')]+template |
| | 444 | template = func.__module__[:func.__module__.rfind('.')] + template |
| 444 | 445 | |
| 445 | | return _process_output(output, template, format, content_type, mapping, |
| 446 | | fragment) |
| | 446 | return _process_output(output, template, format, |
| | 447 | content_type, fragment, **options) |
| 447 | 448 | |
| 448 | 449 | |
| 449 | 450 | def flash(message): |
-
|
|
|
|
| 106 | 106 | return engine, template, enginename |
| 107 | 107 | |
| 108 | 108 | |
| 109 | | def render(info, template=None, format=None, headers=None, mapping=None, |
| 110 | | fragment=False): |
| | 109 | def render(info, template=None, format=None, |
| | 110 | headers=None, fragment=False, **options): |
| 111 | 111 | """Renders data in the desired format. |
| 112 | 112 | |
| 113 | 113 | @param info: the data itself |
| … |
… |
|
| 122 | 122 | @param headers: for response headers, primarily the content type |
| 123 | 123 | @type headers: dict |
| 124 | 124 | |
| 125 | | @param mapping: keyword arguments passed to the underlying engine |
| 126 | | @type mapping: ? |
| 127 | | |
| 128 | 125 | @param fragment: passed through to tell the template if only a |
| 129 | 126 | fragment of a page is desired. This is a way to allow |
| 130 | 127 | xml template engines to generate non valid html/xml |
| 131 | 128 | because you warn them to not bother about it. |
| 132 | 129 | @type fragment: bool |
| | 130 | |
| | 131 | All additional options are passed as keyword parameters |
| | 132 | to the render method of the template engine. |
| | 133 | |
| 133 | 134 | """ |
| 134 | 135 | environ = getattr(cherrypy.request, 'wsgi_environ', {}) |
| 135 | 136 | if environ.get('paste.testing', False): |
| … |
… |
|
| 191 | 192 | |
| 192 | 193 | headers['Content-Type'] = content_type |
| 193 | 194 | |
| 194 | | args, kw = adapt_call(engine.render, args=[], |
| 195 | | kw = dict(info=info, format=format, fragment=fragment, |
| 196 | | template=template, mapping=mapping), start=1) |
| | 195 | kw = dict(info=info, format=format, fragment=fragment, template=template) |
| | 196 | kw.update(options) |
| | 197 | args, kw = adapt_call(engine.render, args=[], kw=kw, start=1) |
| 197 | 198 | |
| 198 | 199 | return engine.render(**kw) |
| 199 | 200 | |
Download in other formats: