Commits

Armin Ronacher committed f34c028

Added template tests and made config a true global

  • Participants
  • Parent commits 5e88c81

Comments (0)

Files changed (5)

 - 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
 -----------

File docs/templating.rst

 
    .. 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:

File flask/app.py

         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
                         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())

File flask/templating.py

 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
 
     `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
     :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)
     :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)

File flask/testsuite/templating.py

         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'