]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Let the Environment override the Context
authorThiefMaster <adrian@planetcoding.net>
Mon, 6 Apr 2015 12:08:46 +0000 (14:08 +0200)
committerThiefMaster <adrian@planetcoding.net>
Mon, 6 Apr 2015 12:19:43 +0000 (14:19 +0200)
closes #404

docs/api.rst
jinja2/environment.py
jinja2/runtime.py
tests/test_api.py

index aaf262b34d6a371b6cca1d78969b73d1e69bc2d4..3fa806190cdc2670964b2f6e4952310f4db64073 100644 (file)
@@ -160,6 +160,13 @@ useful if you want to dig deeper into Jinja2 or :ref:`develop extensions
        in most cases, unless you need to modify the Python code a
        template compiles to.
 
+    .. attribute:: context_class
+
+       The context used for templates.  This should not be changed
+       in most cases, unless you need to modify internals of how
+       template variables are handled.  For details, see
+       :class:`~jinja2.runtime.Context`.
+
     .. automethod:: overlay([options])
 
     .. method:: undefined([hint, obj, name, exc])
index 3bd8fa7b49a3cbdc38f288aa43734d1a750c6b95..8b2572bb8c176ea89c9ecdf15466642be6ab9e6f 100644 (file)
@@ -22,7 +22,7 @@ from jinja2.parser import Parser
 from jinja2.nodes import EvalContext
 from jinja2.optimizer import optimize
 from jinja2.compiler import generate, CodeGenerator
-from jinja2.runtime import Undefined, new_context
+from jinja2.runtime import Undefined, new_context, Context
 from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \
      TemplatesNotFound, TemplateRuntimeError
 from jinja2.utils import import_string, LRUCache, Markup, missing, \
@@ -242,6 +242,10 @@ class Environment(object):
     #: :class:`~jinja2.compiler.CodeGenerator` for more information.
     code_generator_class = CodeGenerator
 
+    #: the context class thatis used for templates.  See
+    #: :class:`~jinja2.runtime.Context` for more information.
+    context_class = Context
+
     def __init__(self,
                  block_start_string=BLOCK_START_STRING,
                  block_end_string=BLOCK_END_STRING,
index 9e818df10d3087d7333d01fc8a8e9a7bd84bb7d7..685a12da068c4808f2dfcec64d68e581f47e26fe 100644 (file)
@@ -69,7 +69,8 @@ def new_context(environment, template_name, blocks, vars=None,
         for key, value in iteritems(locals):
             if key[:2] == 'l_' and value is not missing:
                 parent[key[2:]] = value
-    return Context(environment, parent, template_name, blocks)
+    return environment.context_class(environment, parent, template_name,
+                                     blocks)
 
 
 class TemplateReference(object):
index 5cd7f86f25d1afdccb8072f7c6f8eff2727bd120..99d8dc1e13ea9994cbb3f24e18977ded074d5d03 100644 (file)
@@ -17,6 +17,7 @@ from jinja2 import Environment, Undefined, DebugUndefined, \
      StrictUndefined, UndefinedError, meta, \
      is_undefined, Template, DictLoader, make_logging_undefined
 from jinja2.compiler import CodeGenerator
+from jinja2.runtime import Context
 from jinja2.utils import Cycler
 
 
@@ -312,3 +313,15 @@ class TestLowLevel():
         env = CustomEnvironment()
         tmpl = env.from_string('{% set foo = "foo" %}{{ foo }}')
         assert tmpl.render() == 'bar'
+
+    def test_custom_context(self):
+        class CustomContext(Context):
+            def resolve(self, key):
+                return 'resolve-' + key
+
+        class CustomEnvironment(Environment):
+            context_class = CustomContext
+
+        env = CustomEnvironment()
+        tmpl = env.from_string('{{ foo }}')
+        assert tmpl.render() == 'resolve-foo'