Added template tests and made config a true global
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 5e88c81 · Browse files · 5 files·+43 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+43 added−11 removed
- other:+43 added−11 removed
Provenance
- Commit
f34c028- Tree
2dbf319- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5e88c81
5 files changed+43 added−11 removed
modified:docs/templating.rst+12 added-3 removed
36 unmodified lines37 The current configuration object (:data:`flask.config`)38 39 .. versionadded:: 0.640 41+ .. versionchanged:: 0.1042+ This is now always available, even in imported templates.43+44 .. data:: request45 :noindex:46 44- The current request object (:class:`flask.request`)47+ The current request object (:class:`flask.request`). This variable is48+ unavailable if the template was rendered without an active request49+ context.50 51 .. data:: session52 :noindex:53 49- The current session object (:class:`flask.session`)54+ The current session object (:class:`flask.session`). This variable55+ is unavailable if the template was rendered without an active request56+ context.57 58 .. data:: g59 :noindex:60 54- The request-bound object for global variables (:data:`flask.g`)61+ The request-bound object for global variables (:data:`flask.g`). This62+ variable is unavailable if the template was rendered without an active63+ request context.64 65 .. function:: url_for66 :noindex:67 157 unmodified linesmodified:flask/testsuite/templating.py+12 added
35 unmodified lines36 return flask.render_template_string('{{ config }}', config=42)37 rv = app.test_client().get('/')38 self.assert_equal(rv.data, '42')39 40+ def test_request_less_rendering(self):41+ app = flask.Flask(__name__)42+ app.config['WORLD_NAME'] = 'Special World'43+ @app.context_processor44+ def context_processor():45+ return dict(foo=42)46+47+ with app.app_context():48+ rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} '49+ '{{ foo }}')50+ self.assert_equal(rv, 'Hello Special World 42')51+52 def test_standard_context(self):53 app = flask.Flask(__name__)54 app.secret_key = 'development key'55 @app.route('/')237 unmodified linesmodified:flask/app.py+7 added-4 removed
643 unmodified lines644 options['autoescape'] = self.select_jinja_autoescape645 rv = Environment(self, **options)646 rv.globals.update(647 url_for=url_for,648- get_flashed_messages=get_flashed_messages648+ get_flashed_messages=get_flashed_messages,649+ config=self.config650 )651 rv.filters['tojson'] = json.htmlsafe_dumps652 return rv653 40 unmodified lines694 :param context: the context as a dictionary that is updated in place695 to add extra variables.696 """697 funcs = self.template_context_processors[None]698+ reqctx = _request_ctx_stack.top699+ if reqctx is not None:697- bp = _request_ctx_stack.top.request.blueprint698- if bp is not None and bp in self.template_context_processors:699- funcs = chain(funcs, self.template_context_processors[bp])700+ bp = reqctx.request.blueprint701+ if bp is not None and bp in self.template_context_processors:702+ funcs = chain(funcs, self.template_context_processors[bp])703 orig_ctx = context.copy()704 for func in funcs:705 context.update(func())706 # make sure the original values win. This makes it possible to1051 unmodified linesmodified:flask/templating.py+5 added-4 removed
10 unmodified lines11 import posixpath12 from jinja2 import BaseLoader, Environment as BaseEnvironment, \13 TemplateNotFound14 15-from .globals import _request_ctx_stack15+from .globals import _request_ctx_stack, _app_ctx_stack16 from .signals import template_rendered17 from .module import blueprint_is_module18 19 1 unmodified line21 """Default template context processor. Injects `request`,22 `session` and `g`.23 """24 reqctx = _request_ctx_stack.top25+ if reqctx is None:26+ return {}27 return dict(26- config=reqctx.app.config,28 request=reqctx.request,29 session=reqctx.session,30 g=reqctx.g31 )87 unmodified lines119 the first one existing will be rendered120 :param context: the variables that should be available in the121 context of the template.122 """122- ctx = _request_ctx_stack.top123+ ctx = _app_ctx_stack.top124 ctx.app.update_template_context(context)125 return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),126 context, ctx.app)127 6 unmodified lines134 rendered135 :param context: the variables that should be available in the136 context of the template.137 """137- ctx = _request_ctx_stack.top138+ ctx = _app_ctx_stack.top139 ctx.app.update_template_context(context)140 return _render(ctx.app.jinja_env.from_string(source),141 context, ctx.app)modified:CHANGES+7 added
21 unmodified lines22 JSON is handled by Flask or any Flask extension.23 - Removed deprecated internal ``flask.session`` module alias. Use24 ``flask.sessions`` instead to get the session module. This is not to25 be confused with ``flask.session`` the session proxy.26+- Templates can now be rendered without request context. The behavior is27+ slightly different as the ``request``, ``session`` and ``g`` objects28+ will not be available and blueprint's context processors are not29+ called.30+- The config object is now available to the template as a real global and31+ not through a context processor which makes it available even in imported32+ templates by default.33 34 Version 0.935 -----------36 365 unmodified lines