Fixed a documentation error and implemented template context processors.
This commit is contained in:
parent
36071ca1f4
commit
a01e8b49ca
1 changed files with 28 additions and 4 deletions
32
flask.py
32
flask.py
|
|
@ -130,6 +130,18 @@ def render_template_string(source, **context):
|
|||
return current_app.jinja_env.from_string(source).render(context)
|
||||
|
||||
|
||||
def _default_template_ctx_processor():
|
||||
"""Default template context processor. Injects `request`,
|
||||
`session` and `g`.
|
||||
"""
|
||||
reqctx = _request_ctx_stack.top
|
||||
return dict(
|
||||
request=reqctx.request,
|
||||
session=reqctx.session,
|
||||
g=reqctx.g
|
||||
)
|
||||
|
||||
|
||||
class Flask(object):
|
||||
"""The flask object implements a WSGI application and acts as the central
|
||||
object. It is passed the name of the module or package of the
|
||||
|
|
@ -216,6 +228,14 @@ class Flask(object):
|
|||
#: To register a function here use the :meth:`request_shtdown`
|
||||
#: decorator.
|
||||
self.request_shutdown_funcs = []
|
||||
|
||||
#: a list of functions that are called without arguments
|
||||
#: to populate the template context. Each returns a dictionary
|
||||
#: that the template context is updated with.
|
||||
#: To register a function here, use the :meth:`context_processor`
|
||||
#: decorator.
|
||||
self.template_context_processors = [_default_template_ctx_processor]
|
||||
|
||||
self.url_map = Map()
|
||||
|
||||
if self.static_path is not None:
|
||||
|
|
@ -248,9 +268,8 @@ class Flask(object):
|
|||
to add extra variables.
|
||||
"""
|
||||
reqctx = _request_ctx_stack.top
|
||||
context['request'] = reqctx.request
|
||||
context['session'] = reqctx.session
|
||||
context['g'] = reqctx.g
|
||||
for func in self.template_context_processors:
|
||||
context.update(func())
|
||||
|
||||
def run(self, host='localhost', port=5000, **options):
|
||||
"""Runs the application on a local development server. If the
|
||||
|
|
@ -325,7 +344,7 @@ class Flask(object):
|
|||
:param session: the session to be saved (a
|
||||
:class:`~werkzeug.contrib.securecookie.SecureCookie`
|
||||
object)
|
||||
:param request: an instance of :attr:`response_class`
|
||||
:param response: an instance of :attr:`response_class`
|
||||
"""
|
||||
if session is not None:
|
||||
session.save_cookie(response, self.session_cookie_name)
|
||||
|
|
@ -462,6 +481,11 @@ class Flask(object):
|
|||
self.request_shutdown_funcs.append(f)
|
||||
return f
|
||||
|
||||
def context_processor(self, f):
|
||||
"""Registers a template context processor function."""
|
||||
self.template_context_processors.append(f)
|
||||
return f
|
||||
|
||||
def match_request(self):
|
||||
"""Matches the current request against the URL map and also
|
||||
stores the endpoint and view arguments on the request object
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue