mirror of https://github.com/pallets/flask.git
Added template tests and made config a true global
This commit is contained in:
parent
5e88c8184d
commit
f34c028125
7
CHANGES
7
CHANGES
|
@ -23,6 +23,13 @@ Release date to be decided.
|
||||||
- Removed deprecated internal ``flask.session`` module alias. Use
|
- Removed deprecated internal ``flask.session`` module alias. Use
|
||||||
``flask.sessions`` instead to get the session module. This is not to
|
``flask.sessions`` instead to get the session module. This is not to
|
||||||
be confused with ``flask.session`` the session proxy.
|
be confused with ``flask.session`` the session proxy.
|
||||||
|
- Templates can now be rendered without request context. The behavior is
|
||||||
|
slightly different as the ``request``, ``session`` and ``g`` objects
|
||||||
|
will not be available and blueprint's context processors are not
|
||||||
|
called.
|
||||||
|
- The config object is now available to the template as a real global and
|
||||||
|
not through a context processor which makes it available even in imported
|
||||||
|
templates by default.
|
||||||
|
|
||||||
Version 0.9
|
Version 0.9
|
||||||
-----------
|
-----------
|
||||||
|
|
|
@ -38,20 +38,29 @@ by default:
|
||||||
|
|
||||||
.. versionadded:: 0.6
|
.. versionadded:: 0.6
|
||||||
|
|
||||||
|
.. versionchanged:: 0.10
|
||||||
|
This is now always available, even in imported templates.
|
||||||
|
|
||||||
.. data:: request
|
.. data:: request
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
The current request object (:class:`flask.request`)
|
The current request object (:class:`flask.request`). This variable is
|
||||||
|
unavailable if the template was rendered without an active request
|
||||||
|
context.
|
||||||
|
|
||||||
.. data:: session
|
.. data:: session
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
The current session object (:class:`flask.session`)
|
The current session object (:class:`flask.session`). This variable
|
||||||
|
is unavailable if the template was rendered without an active request
|
||||||
|
context.
|
||||||
|
|
||||||
.. data:: g
|
.. data:: g
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
The request-bound object for global variables (:data:`flask.g`)
|
The request-bound object for global variables (:data:`flask.g`). This
|
||||||
|
variable is unavailable if the template was rendered without an active
|
||||||
|
request context.
|
||||||
|
|
||||||
.. function:: url_for
|
.. function:: url_for
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
11
flask/app.py
11
flask/app.py
|
@ -645,7 +645,8 @@ class Flask(_PackageBoundObject):
|
||||||
rv = Environment(self, **options)
|
rv = Environment(self, **options)
|
||||||
rv.globals.update(
|
rv.globals.update(
|
||||||
url_for=url_for,
|
url_for=url_for,
|
||||||
get_flashed_messages=get_flashed_messages
|
get_flashed_messages=get_flashed_messages,
|
||||||
|
config=self.config
|
||||||
)
|
)
|
||||||
rv.filters['tojson'] = json.htmlsafe_dumps
|
rv.filters['tojson'] = json.htmlsafe_dumps
|
||||||
return rv
|
return rv
|
||||||
|
@ -694,9 +695,11 @@ class Flask(_PackageBoundObject):
|
||||||
to add extra variables.
|
to add extra variables.
|
||||||
"""
|
"""
|
||||||
funcs = self.template_context_processors[None]
|
funcs = self.template_context_processors[None]
|
||||||
bp = _request_ctx_stack.top.request.blueprint
|
reqctx = _request_ctx_stack.top
|
||||||
if bp is not None and bp in self.template_context_processors:
|
if reqctx is not None:
|
||||||
funcs = chain(funcs, self.template_context_processors[bp])
|
bp = reqctx.request.blueprint
|
||||||
|
if bp is not None and bp in self.template_context_processors:
|
||||||
|
funcs = chain(funcs, self.template_context_processors[bp])
|
||||||
orig_ctx = context.copy()
|
orig_ctx = context.copy()
|
||||||
for func in funcs:
|
for func in funcs:
|
||||||
context.update(func())
|
context.update(func())
|
||||||
|
|
|
@ -12,7 +12,7 @@ import posixpath
|
||||||
from jinja2 import BaseLoader, Environment as BaseEnvironment, \
|
from jinja2 import BaseLoader, Environment as BaseEnvironment, \
|
||||||
TemplateNotFound
|
TemplateNotFound
|
||||||
|
|
||||||
from .globals import _request_ctx_stack
|
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||||
from .signals import template_rendered
|
from .signals import template_rendered
|
||||||
from .module import blueprint_is_module
|
from .module import blueprint_is_module
|
||||||
|
|
||||||
|
@ -22,8 +22,9 @@ def _default_template_ctx_processor():
|
||||||
`session` and `g`.
|
`session` and `g`.
|
||||||
"""
|
"""
|
||||||
reqctx = _request_ctx_stack.top
|
reqctx = _request_ctx_stack.top
|
||||||
|
if reqctx is None:
|
||||||
|
return {}
|
||||||
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
|
||||||
|
@ -119,7 +120,7 @@ def render_template(template_name_or_list, **context):
|
||||||
:param context: the variables that should be available in the
|
:param context: the variables that should be available in the
|
||||||
context of the template.
|
context of the template.
|
||||||
"""
|
"""
|
||||||
ctx = _request_ctx_stack.top
|
ctx = _app_ctx_stack.top
|
||||||
ctx.app.update_template_context(context)
|
ctx.app.update_template_context(context)
|
||||||
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
|
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
|
||||||
context, ctx.app)
|
context, ctx.app)
|
||||||
|
@ -134,7 +135,7 @@ def render_template_string(source, **context):
|
||||||
:param context: the variables that should be available in the
|
:param context: the variables that should be available in the
|
||||||
context of the template.
|
context of the template.
|
||||||
"""
|
"""
|
||||||
ctx = _request_ctx_stack.top
|
ctx = _app_ctx_stack.top
|
||||||
ctx.app.update_template_context(context)
|
ctx.app.update_template_context(context)
|
||||||
return _render(ctx.app.jinja_env.from_string(source),
|
return _render(ctx.app.jinja_env.from_string(source),
|
||||||
context, ctx.app)
|
context, ctx.app)
|
||||||
|
|
|
@ -37,6 +37,18 @@ class TemplatingTestCase(FlaskTestCase):
|
||||||
rv = app.test_client().get('/')
|
rv = app.test_client().get('/')
|
||||||
self.assert_equal(rv.data, '42')
|
self.assert_equal(rv.data, '42')
|
||||||
|
|
||||||
|
def test_request_less_rendering(self):
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
app.config['WORLD_NAME'] = 'Special World'
|
||||||
|
@app.context_processor
|
||||||
|
def context_processor():
|
||||||
|
return dict(foo=42)
|
||||||
|
|
||||||
|
with app.app_context():
|
||||||
|
rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} '
|
||||||
|
'{{ foo }}')
|
||||||
|
self.assert_equal(rv, 'Hello Special World 42')
|
||||||
|
|
||||||
def test_standard_context(self):
|
def test_standard_context(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.secret_key = 'development key'
|
app.secret_key = 'development key'
|
||||||
|
|
Loading…
Reference in New Issue