Skip to content
Myr

before_render_template signal can override render template.

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

authored by Alexander Pantyukhin · committed by Markus Unterwaditzer · parent 1fbeb33 · Browse files · 2 files+29 added−2 removed
This commit

State

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

Diff stats

2 files+29 added−2 removed

  • tests:+16 added−0 removed
  • other:+13 added−2 removed

2 files+29 added−2 removed

modified:flask/templating.py+13 added-2 removed
99 unmodified lines
100 return list(result)101102103def _render(template, context, app):104 """Renders the template and fires the signal"""105 before_render_template.send(app, template=template, context=context)104 """Renders the template and fires signals.105106 It is possible to override render template in the before_render_template signal.107 It can be done only if exactly one receiver and it return not None result."""108109 brt_resp = before_render_template.send(app, template=template, context=context)110 111 if len(brt_resp) == 1:112 first_resp = brt_resp[0]113114 if len(first_resp) == 2 and first_resp[1] is not None:115 return first_resp[1]116117 rv = template.render(context)118 template_rendered.send(app, template=template, context=context)119 return rv120
30 unmodified lines
modified:tests/test_signals.py+16 added
65 unmodified lines
66 assert template.name == 'simple_template.html'67 assert context['whiskey'] == 4368 assert rv.data == b'<h1>43</h1>'6970def test_before_render_template_signal_not_None_result_render_skip_render_template():71 app = flask.Flask(__name__)7273 @app.route('/')74 def index():75 return flask.render_template('simple_template.html', whiskey=42)7677 recorded = []7879 def record(sender, template, context):80 recorded.append((template, context))81 return 'Not template string'8283 with flask.before_render_template.connected_to(record):84 rv = app.test_client().get('/')85 assert rv.data == 'Not template string'8687def test_request_signals():88 app = flask.Flask(__name__)89 calls = []90
126 unmodified lines