Skip to content
Myr

Improved the templated decorator in the documentation as recommended by Thadeus Burgess on the mailinglist

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 6b5ba14 · Browse files · 1 file+5 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+5 added−1 removed

  • other:+5 added−1 removed

1 file+5 added−1 removed

modified:docs/patterns/viewdecorators.rst+5 added-1 removed
118 unmodified lines
119As you can see, if no template name is provided it will use the endpoint120of the URL map with dots converted to slashes + ``'.html'``. Otherwise121the provided template name is used. When the decorated function returns,122the dictionary returned is passed to the template rendering function. If123`None` is returned, an empty dictionary is assumed.123`None` is returned, an empty dictionary is assumed, if something else than124a dictionary is returned we return it from the function unchanged. That125way you can still use the redirect function or return simple strings.126127Here the code for that decorator::128129 from functools import wraps
9 unmodified lines
139 .replace('.', '/') + '.html'140 ctx = f(*args, **kwargs)141 if ctx is None:142 ctx = {}143 elif not isinstance(ctx, dict):144 return ctx145 return render_template(template_name, **ctx)146 return decorated_function147 return decorator