Skip to content
Myr

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

5 files+43 added−11 removed

modified:docs/templating.rst+12 added-3 removed
36 unmodified lines
37 The current configuration object (:data:`flask.config`)3839 .. versionadded:: 0.64041 .. versionchanged:: 0.1042 This is now always available, even in imported templates.4344.. data:: request45 :noindex:4644 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.5051.. data:: session52 :noindex:5349 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.5758.. data:: g59 :noindex:6054 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.6465.. function:: url_for66 :noindex:67
157 unmodified lines
modified:flask/testsuite/templating.py+12 added
35 unmodified lines
36 return flask.render_template_string('{{ config }}', config=42)37 rv = app.test_client().get('/')38 self.assert_equal(rv.data, '42')3940 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)4647 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')5152 def test_standard_context(self):53 app = flask.Flask(__name__)54 app.secret_key = 'development key'55 @app.route('/')
237 unmodified lines
modified:flask/app.py+7 added-4 removed
643 unmodified lines
644 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 lines
694 :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 to
1051 unmodified lines
modified:flask/templating.py+5 added-4 removed
10 unmodified lines
11import posixpath12from jinja2 import BaseLoader, Environment as BaseEnvironment, \13 TemplateNotFound1415from .globals import _request_ctx_stack15from .globals import _request_ctx_stack, _app_ctx_stack16from .signals import template_rendered17from .module import blueprint_is_module1819
1 unmodified line
21 """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 lines
119 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 lines
134 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 lines
22 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.3334Version 0.935-----------36
365 unmodified lines