before_render_template signal can override render template.

This commit is contained in:
Alexander Pantyukhin 2015-03-24 12:20:28 +00:00 committed by Markus Unterwaditzer
parent 1fbeb337c4
commit 967907ee81
2 changed files with 29 additions and 2 deletions

View file

@ -101,8 +101,19 @@ class DispatchingJinjaLoader(BaseLoader):
def _render(template, context, app):
"""Renders the template and fires the signal"""
before_render_template.send(app, template=template, context=context)
"""Renders the template and fires signals.
It is possible to override render template in the before_render_template signal.
It can be done only if exactly one receiver and it return not None result."""
brt_resp = before_render_template.send(app, template=template, context=context)
if len(brt_resp) == 1:
first_resp = brt_resp[0]
if len(first_resp) == 2 and first_resp[1] is not None:
return first_resp[1]
rv = template.render(context)
template_rendered.send(app, template=template, context=context)
return rv