diff --git a/flask/__init__.py b/flask/__init__.py index a6ef98ca..7fd7a253 100644 --- a/flask/__init__.py +++ b/flask/__init__.py @@ -34,7 +34,7 @@ from .templating import render_template, render_template_string from .signals import signals_available, template_rendered, request_started, \ request_finished, got_request_exception, request_tearing_down, \ appcontext_tearing_down, appcontext_pushed, \ - appcontext_popped, message_flashed + appcontext_popped, message_flashed, before_render_template # We're not exposing the actual json module but a convenient wrapper around # it. diff --git a/flask/signals.py b/flask/signals.py index ca1c1d90..4a2cfe07 100644 --- a/flask/signals.py +++ b/flask/signals.py @@ -45,6 +45,7 @@ _signals = Namespace() # Core signals. For usage examples grep the source code or consult # the API documentation in docs/api.rst as well as docs/signals.rst template_rendered = _signals.signal('template-rendered') +before_render_template = _signals.signal('before-render-template') request_started = _signals.signal('request-started') request_finished = _signals.signal('request-finished') request_tearing_down = _signals.signal('request-tearing-down') @@ -52,4 +53,4 @@ got_request_exception = _signals.signal('got-request-exception') appcontext_tearing_down = _signals.signal('appcontext-tearing-down') appcontext_pushed = _signals.signal('appcontext-pushed') appcontext_popped = _signals.signal('appcontext-popped') -message_flashed = _signals.signal('message-flashed') +message_flashed = _signals.signal('message-flashed') \ No newline at end of file diff --git a/flask/templating.py b/flask/templating.py index 1e39b932..74ff104f 100644 --- a/flask/templating.py +++ b/flask/templating.py @@ -12,7 +12,7 @@ from jinja2 import BaseLoader, Environment as BaseEnvironment, \ TemplateNotFound from .globals import _request_ctx_stack, _app_ctx_stack -from .signals import template_rendered +from .signals import template_rendered, before_render_template def _default_template_ctx_processor(): @@ -102,6 +102,7 @@ class DispatchingJinjaLoader(BaseLoader): def _render(template, context, app): """Renders the template and fires the signal""" + before_render_template.send(app, template=template, context=context) rv = template.render(context) template_rendered.send(app, template=template, context=context) return rv diff --git a/tests/test_signals.py b/tests/test_signals.py index e17acfd4..b687b6e8 100644 --- a/tests/test_signals.py +++ b/tests/test_signals.py @@ -46,6 +46,30 @@ def test_template_rendered(): finally: flask.template_rendered.disconnect(record, app) +def test_before_render_template(): + app = flask.Flask(__name__) + + @app.route('/') + def index(): + return flask.render_template('simple_template.html', whiskey=42) + + recorded = [] + + def record(sender, template, context): + context['whiskey'] = 43 + recorded.append((template, context)) + + flask.before_render_template.connect(record, app) + try: + rv = app.test_client().get('/') + assert len(recorded) == 1 + template, context = recorded[0] + assert template.name == 'simple_template.html' + assert context['whiskey'] == 43 + assert rv.data == b'

43

' + finally: + flask.before_render_template.disconnect(record, app) + def test_request_signals(): app = flask.Flask(__name__) calls = []