From: ThiefMaster Date: Mon, 6 Apr 2015 12:08:46 +0000 (+0200) Subject: Let the Environment override the Context X-Git-Tag: 2.8~18^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f22fdd5ffe81aab743f78290071b0aa506705533;p=thirdparty%2Fjinja.git Let the Environment override the Context closes #404 --- diff --git a/docs/api.rst b/docs/api.rst index aaf262b3..3fa80619 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -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]) diff --git a/jinja2/environment.py b/jinja2/environment.py index 3bd8fa7b..8b2572bb 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -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, diff --git a/jinja2/runtime.py b/jinja2/runtime.py index 9e818df1..685a12da 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -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): diff --git a/tests/test_api.py b/tests/test_api.py index 5cd7f86f..99d8dc1e 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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'