Permalink
Browse files

Added template tests and made config a true global

  • Loading branch information...
1 parent 5e88c81 commit f34c0281252bf1838e2ec24fe8b064b232a098ef @mitsuhiko mitsuhiko committed Oct 8, 2012
Showing with 43 additions and 11 deletions.
  1. +7 −0 CHANGES
  2. +12 −3 docs/templating.rst
  3. +7 −4 flask/app.py
  4. +5 −4 flask/templating.py
  5. +12 −0 flask/testsuite/templating.py
View
@@ -23,6 +23,13 @@ Release date to be decided.
- Removed deprecated internal ``flask.session`` module alias. Use
``flask.sessions`` instead to get the session module. This is not to
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
-----------
View
@@ -38,20 +38,29 @@ by default:
.. versionadded:: 0.6
+ .. versionchanged:: 0.10
+ This is now always available, even in imported templates.
+
.. data:: request
: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
: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
: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
:noindex:
View
@@ -645,7 +645,8 @@ def create_jinja_environment(self):
rv = Environment(self, **options)
rv.globals.update(
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
return rv
@@ -694,9 +695,11 @@ def update_template_context(self, context):
to add extra variables.
"""
funcs = self.template_context_processors[None]
- bp = _request_ctx_stack.top.request.blueprint
- if bp is not None and bp in self.template_context_processors:
- funcs = chain(funcs, self.template_context_processors[bp])
+ reqctx = _request_ctx_stack.top
+ if reqctx is not None:
+ 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()
for func in funcs:
context.update(func())
View
@@ -12,7 +12,7 @@
from jinja2 import BaseLoader, Environment as BaseEnvironment, \
TemplateNotFound
-from .globals import _request_ctx_stack
+from .globals import _request_ctx_stack, _app_ctx_stack
from .signals import template_rendered
from .module import blueprint_is_module
@@ -22,8 +22,9 @@ def _default_template_ctx_processor():
`session` and `g`.
"""
reqctx = _request_ctx_stack.top
+ if reqctx is None:
+ return {}
return dict(
- config=reqctx.app.config,
request=reqctx.request,
session=reqctx.session,
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
context of the template.
"""
- ctx = _request_ctx_stack.top
+ ctx = _app_ctx_stack.top
ctx.app.update_template_context(context)
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
context, ctx.app)
@@ -134,7 +135,7 @@ def render_template_string(source, **context):
:param context: the variables that should be available in the
context of the template.
"""
- ctx = _request_ctx_stack.top
+ ctx = _app_ctx_stack.top
ctx.app.update_template_context(context)
return _render(ctx.app.jinja_env.from_string(source),
context, ctx.app)
@@ -37,6 +37,18 @@ def index():
rv = app.test_client().get('/')
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):
app = flask.Flask(__name__)
app.secret_key = 'development key'

0 comments on commit f34c028

Please sign in to comment.