Skip to content
Myr

before_render_template signal

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 cb9f645 · Browse files · 4 files+29 added−3 removed
This commit

State

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

Diff stats

4 files+29 added−3 removed

  • tests:+24 added−0 removed
  • other:+5 added−3 removed

4 files+29 added−3 removed

modified:flask/__init__.py+1 added-1 removed
32 unmodified lines
33# the signals34from .signals import signals_available, template_rendered, request_started, \35 request_finished, got_request_exception, request_tearing_down, \36 appcontext_tearing_down, appcontext_pushed, \37 appcontext_popped, message_flashed37 appcontext_popped, message_flashed, before_render_template3839# We're not exposing the actual json module but a convenient wrapper around40# it.41from . import json
8 unmodified lines
modified:flask/signals.py+2 added-1 removed
43 unmodified lines
4445# Core signals. For usage examples grep the source code or consult46# the API documentation in docs/api.rst as well as docs/signals.rst47template_rendered = _signals.signal('template-rendered')48before_render_template = _signals.signal('before-render-template')49request_started = _signals.signal('request-started')50request_finished = _signals.signal('request-finished')51request_tearing_down = _signals.signal('request-tearing-down')52got_request_exception = _signals.signal('got-request-exception')53appcontext_tearing_down = _signals.signal('appcontext-tearing-down')54appcontext_pushed = _signals.signal('appcontext-pushed')55appcontext_popped = _signals.signal('appcontext-popped')55message_flashed = _signals.signal('message-flashed')56message_flashed = _signals.signal('message-flashed')
modified:flask/templating.py+2 added-1 removed
10 unmodified lines
11from jinja2 import BaseLoader, Environment as BaseEnvironment, \12 TemplateNotFound1314from .globals import _request_ctx_stack, _app_ctx_stack15from .signals import template_rendered15from .signals import template_rendered, before_render_template161718def _default_template_ctx_processor():19 """Default template context processor. Injects `request`,
81 unmodified lines
101102103def _render(template, context, app):104 """Renders the template and fires the signal"""105 before_render_template.send(app, template=template, context=context)106 rv = template.render(context)107 template_rendered.send(app, template=template, context=context)108 return rv109
30 unmodified lines
modified:tests/test_signals.py+24 added
44 unmodified lines
45 assert context['whiskey'] == 4246 finally:47 flask.template_rendered.disconnect(record, app)4849def test_before_render_template():50 app = flask.Flask(__name__)5152 @app.route('/')53 def index():54 return flask.render_template('simple_template.html', whiskey=42)5556 recorded = []5758 def record(sender, template, context):59 context['whiskey'] = 4360 recorded.append((template, context))6162 flask.before_render_template.connect(record, app)63 try:64 rv = app.test_client().get('/')65 assert len(recorded) == 166 template, context = recorded[0]67 assert template.name == 'simple_template.html'68 assert context['whiskey'] == 4369 assert rv.data == b'<h1>43</h1>'70 finally:71 flask.before_render_template.disconnect(record, app)7273def test_request_signals():74 app = flask.Flask(__name__)75 calls = []76
126 unmodified lines