Fixed a documentation error and implemented template context processors.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 36071ca · Browse files · 1 file·+28 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+28 added−4 removed
- other:+28 added−4 removed
Provenance
- Commit
a01e8b4- Tree
b446fdf- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
36071ca
1 file changed+28 added−4 removed
modified:flask.py+28 added-4 removed
128 unmodified lines129 current_app.update_template_context(context)130 return current_app.jinja_env.from_string(source).render(context)131 132 133+def _default_template_ctx_processor():134+ """Default template context processor. Injects `request`,135+ `session` and `g`.136+ """137+ reqctx = _request_ctx_stack.top138+ return dict(139+ request=reqctx.request,140+ session=reqctx.session,141+ g=reqctx.g142+ )143+144+145 class Flask(object):146 """The flask object implements a WSGI application and acts as the central147 object. It is passed the name of the module or package of the148 application. Once it is created it will act as a central registry for78 unmodified lines227 #: object and modify it in place or replace it.228 #: To register a function here use the :meth:`request_shtdown`229 #: decorator.230 self.request_shutdown_funcs = []231+232+ #: a list of functions that are called without arguments233+ #: to populate the template context. Each returns a dictionary234+ #: that the template context is updated with.235+ #: To register a function here, use the :meth:`context_processor`236+ #: decorator.237+ self.template_context_processors = [_default_template_ctx_processor]238+239 self.url_map = Map()240 241 if self.static_path is not None:242 self.url_map.add(Rule(self.static_path + '/<filename>',24 unmodified lines267 :param context: the context as a dictionary that is updated in place268 to add extra variables.269 """270 reqctx = _request_ctx_stack.top251- context['request'] = reqctx.request252- context['session'] = reqctx.session253- context['g'] = reqctx.g271+ for func in self.template_context_processors:272+ context.update(func())273 274 def run(self, host='localhost', port=5000, **options):275 """Runs the application on a local development server. If the276 :attr:`debug` flag is set the server will automatically reload66 unmodified lines343 344 :param session: the session to be saved (a345 :class:`~werkzeug.contrib.securecookie.SecureCookie`346 object)328- :param request: an instance of :attr:`response_class`347+ :param response: an instance of :attr:`response_class`348 """349 if session is not None:350 session.save_cookie(response, self.session_cookie_name)351 128 unmodified lines480 """Register a function to be run after each request."""481 self.request_shutdown_funcs.append(f)482 return f483 484+ def context_processor(self, f):485+ """Registers a template context processor function."""486+ self.template_context_processors.append(f)487+ return f488+489 def match_request(self):490 """Matches the current request against the URL map and also491 stores the endpoint and view arguments on the request object492 is successful, otherwise the exception is stored.137 unmodified lines