Browse Source

Config is now available in templates, context processors no longer override keys

pull/112/head
Armin Ronacher 14 years ago
parent
commit
aa3d8398fd
  1. 4
      CHANGES
  2. 8
      docs/upgrading.rst
  3. 11
      flask/app.py
  4. 1
      flask/templating.py
  5. 24
      tests/flask_tests.py

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 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 remove the static folder if it's part of a mapping in the .yml
file. 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 Version 0.5.1
------------- -------------

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 dependend on the order of execution of post-request functions, be sure to
change the order. 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 Version 0.5
----------- -----------

11
flask/app.py

@ -343,7 +343,11 @@ class Flask(_PackageBoundObject):
def update_template_context(self, context): def update_template_context(self, context):
"""Update the template context with some commonly used variables. """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 :param context: the context as a dictionary that is updated in place
to add extra variables. to add extra variables.
@ -352,8 +356,13 @@ class Flask(_PackageBoundObject):
mod = _request_ctx_stack.top.request.module mod = _request_ctx_stack.top.request.module
if mod is not None and mod in self.template_context_processors: if mod is not None and mod in self.template_context_processors:
funcs = chain(funcs, self.template_context_processors[mod]) funcs = chain(funcs, self.template_context_processors[mod])
orig_ctx = context.copy()
for func in funcs: for func in funcs:
context.update(func()) 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): def run(self, host='127.0.0.1', port=5000, **options):
"""Runs the application on a local development server. If the """Runs the application on a local development server. If the

1
flask/templating.py

@ -19,6 +19,7 @@ def _default_template_ctx_processor():
""" """
reqctx = _request_ctx_stack.top reqctx = _request_ctx_stack.top
return dict( return dict(
config=reqctx.app.config,
request=reqctx.request, request=reqctx.request,
session=reqctx.session, session=reqctx.session,
g=reqctx.g g=reqctx.g

24
tests/flask_tests.py

@ -492,6 +492,30 @@ class TemplatingTestCase(unittest.TestCase):
rv = app.test_client().get('/') rv = app.test_client().get('/')
assert rv.data == '<p>23|42' 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): def test_escaping(self):
text = '<p>Hello World!' text = '<p>Hello World!'
app = flask.Flask(__name__) app = flask.Flask(__name__)

Loading…
Cancel
Save