Skip to content

Commit f34c028

Browse files
committed
Added template tests and made config a true global
1 parent 5e88c81 commit f34c028

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Release date to be decided.
2323
- Removed deprecated internal ``flask.session`` module alias. Use
2424
``flask.sessions`` instead to get the session module. This is not to
2525
be confused with ``flask.session`` the session proxy.
26+
- Templates can now be rendered without request context. The behavior is
27+
slightly different as the ``request``, ``session`` and ``g`` objects
28+
will not be available and blueprint's context processors are not
29+
called.
30+
- The config object is now available to the template as a real global and
31+
not through a context processor which makes it available even in imported
32+
templates by default.
2633

2734
Version 0.9
2835
-----------

docs/templating.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,29 @@ by default:
3838

3939
.. versionadded:: 0.6
4040

41+
.. versionchanged:: 0.10
42+
This is now always available, even in imported templates.
43+
4144
.. data:: request
4245
:noindex:
4346

44-
The current request object (:class:`flask.request`)
47+
The current request object (:class:`flask.request`). This variable is
48+
unavailable if the template was rendered without an active request
49+
context.
4550

4651
.. data:: session
4752
:noindex:
4853

49-
The current session object (:class:`flask.session`)
54+
The current session object (:class:`flask.session`). This variable
55+
is unavailable if the template was rendered without an active request
56+
context.
5057

5158
.. data:: g
5259
:noindex:
5360

54-
The request-bound object for global variables (:data:`flask.g`)
61+
The request-bound object for global variables (:data:`flask.g`). This
62+
variable is unavailable if the template was rendered without an active
63+
request context.
5564

5665
.. function:: url_for
5766
:noindex:

flask/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ def create_jinja_environment(self):
645645
rv = Environment(self, **options)
646646
rv.globals.update(
647647
url_for=url_for,
648-
get_flashed_messages=get_flashed_messages
648+
get_flashed_messages=get_flashed_messages,
649+
config=self.config
649650
)
650651
rv.filters['tojson'] = json.htmlsafe_dumps
651652
return rv
@@ -694,9 +695,11 @@ def update_template_context(self, context):
694695
to add extra variables.
695696
"""
696697
funcs = self.template_context_processors[None]
697-
bp = _request_ctx_stack.top.request.blueprint
698-
if bp is not None and bp in self.template_context_processors:
699-
funcs = chain(funcs, self.template_context_processors[bp])
698+
reqctx = _request_ctx_stack.top
699+
if reqctx is not None:
700+
bp = reqctx.request.blueprint
701+
if bp is not None and bp in self.template_context_processors:
702+
funcs = chain(funcs, self.template_context_processors[bp])
700703
orig_ctx = context.copy()
701704
for func in funcs:
702705
context.update(func())

flask/templating.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from jinja2 import BaseLoader, Environment as BaseEnvironment, \
1313
TemplateNotFound
1414

15-
from .globals import _request_ctx_stack
15+
from .globals import _request_ctx_stack, _app_ctx_stack
1616
from .signals import template_rendered
1717
from .module import blueprint_is_module
1818

@@ -22,8 +22,9 @@ def _default_template_ctx_processor():
2222
`session` and `g`.
2323
"""
2424
reqctx = _request_ctx_stack.top
25+
if reqctx is None:
26+
return {}
2527
return dict(
26-
config=reqctx.app.config,
2728
request=reqctx.request,
2829
session=reqctx.session,
2930
g=reqctx.g
@@ -119,7 +120,7 @@ def render_template(template_name_or_list, **context):
119120
:param context: the variables that should be available in the
120121
context of the template.
121122
"""
122-
ctx = _request_ctx_stack.top
123+
ctx = _app_ctx_stack.top
123124
ctx.app.update_template_context(context)
124125
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
125126
context, ctx.app)
@@ -134,7 +135,7 @@ def render_template_string(source, **context):
134135
:param context: the variables that should be available in the
135136
context of the template.
136137
"""
137-
ctx = _request_ctx_stack.top
138+
ctx = _app_ctx_stack.top
138139
ctx.app.update_template_context(context)
139140
return _render(ctx.app.jinja_env.from_string(source),
140141
context, ctx.app)

flask/testsuite/templating.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def index():
3737
rv = app.test_client().get('/')
3838
self.assert_equal(rv.data, '42')
3939

40+
def test_request_less_rendering(self):
41+
app = flask.Flask(__name__)
42+
app.config['WORLD_NAME'] = 'Special World'
43+
@app.context_processor
44+
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+
4052
def test_standard_context(self):
4153
app = flask.Flask(__name__)
4254
app.secret_key = 'development key'

0 commit comments

Comments
 (0)