Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Config is now available in templates, context processors no longer ov…

…erride keys
  • Loading branch information...
commit aa3d8398fd1a38b449b64fea4979f358f3c711e3 1 parent ed16ae2
@mitsuhiko authored
View
4 CHANGES
@@ -17,6 +17,10 @@ Release date to be announced, codename to be decided.
for the module. This was implemented to aid GAE which will
remove the static folder if it's part of a mapping in the .yml
file.
+- the :attr:`~flask.Flask.config` is now available in the templates
+ as `config`.
+- context processors will no longer override values passed directly
+ to the render function.
Version 0.5.1
-------------
View
8 docs/upgrading.rst
@@ -30,6 +30,14 @@ how other systems handle request pre- and postprocessing. If you
dependend on the order of execution of post-request functions, be sure to
change the order.
+Another change that breaks backwards compatibility is that context
+processors will no longer override values passed directly to the template
+rendering function. If for example `request` is as variable passed
+directly to the template, the default context processor will not override
+it with the current request object. This makes it easier to extend
+context processors later to inject additional variables without breaking
+existing template not expecting them.
+
Version 0.5
-----------
View
11 flask/app.py
@@ -343,7 +343,11 @@ def select_jinja_autoescape(self, filename):
def update_template_context(self, context):
"""Update the template context with some commonly used variables.
- This injects request, session and g into the template context.
+ This injects request, session, config and g into the template
+ context as well as everything template context processors want
+ to inject. Note that the as of Flask 0.6, the original values
+ in the context will not be overriden if a context processor
+ decides to return a value with the same key.
:param context: the context as a dictionary that is updated in place
to add extra variables.
@@ -352,8 +356,13 @@ def update_template_context(self, context):
mod = _request_ctx_stack.top.request.module
if mod is not None and mod in self.template_context_processors:
funcs = chain(funcs, self.template_context_processors[mod])
+ orig_ctx = context.copy()
for func in funcs:
context.update(func())
+ # make sure the original values win. This makes it possible to
+ # easier add new variables in context processors without breaking
+ # existing views.
+ context.update(orig_ctx)
def run(self, host='127.0.0.1', port=5000, **options):
"""Runs the application on a local development server. If the
View
1  flask/templating.py
@@ -19,6 +19,7 @@ def _default_template_ctx_processor():
"""
reqctx = _request_ctx_stack.top
return dict(
+ config=reqctx.app.config,
request=reqctx.request,
session=reqctx.session,
g=reqctx.g
View
24 tests/flask_tests.py
@@ -492,6 +492,30 @@ def index():
rv = app.test_client().get('/')
assert rv.data == '<p>23|42'
+ def test_original_win(self):
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return flask.render_template_string('{{ config }}', config=42)
+ rv = app.test_client().get('/')
+ assert rv.data == '42'
+
+ def test_standard_context(self):
+ app = flask.Flask(__name__)
+ app.secret_key = 'development key'
+ @app.route('/')
+ def index():
+ flask.g.foo = 23
+ flask.session['test'] = 'aha'
+ return flask.render_template_string('''
+ {{ request.args.foo }}
+ {{ g.foo }}
+ {{ config.DEBUG }}
+ {{ session.test }}
+ ''')
+ rv = app.test_client().get('/?foo=42')
+ assert rv.data.split() == ['42', '23', 'False', 'aha']
+
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
Please sign in to comment.
Something went wrong with that request. Please try again.