forked from orbit-oss/flask
before_render_template signal
This commit is contained in:
parent
cb9f645081
commit
d53d5c732b
4 changed files with 29 additions and 3 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'<h1>43</h1>'
|
||||
finally:
|
||||
flask.before_render_template.disconnect(record, app)
|
||||
|
||||
def test_request_signals():
|
||||
app = flask.Flask(__name__)
|
||||
calls = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue