Let the Environment override the Context
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #404
authored by ThiefMaster · committed by ThiefMaster · parent 1493631 · Browse files · 4 files·+27 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+27 added−2 removed
- tests:+13 added−0 removed
- other:+14 added−2 removed
Provenance
- Commit
f22fdd5- Tree
bf84e88- Author
ThiefMaster <adrian@planetcoding.net>- Committer
ThiefMaster <adrian@planetcoding.net>- Parent
1493631
4 files changed+27 added−2 removed
modified:docs/api.rst+7 added
158 unmodified lines159 The class used for code generation. This should not be changed160 in most cases, unless you need to modify the Python code a161 template compiles to.162 163+ .. attribute:: context_class164+165+ The context used for templates. This should not be changed166+ in most cases, unless you need to modify internals of how167+ template variables are handled. For details, see168+ :class:`~jinja2.runtime.Context`.169+170 .. automethod:: overlay([options])171 172 .. method:: undefined([hint, obj, name, exc])173 635 unmodified linesmodified:jinja2/environment.py+5 added-1 removed
20 unmodified lines21 from jinja2.parser import Parser22 from jinja2.nodes import EvalContext23 from jinja2.optimizer import optimize24 from jinja2.compiler import generate, CodeGenerator25-from jinja2.runtime import Undefined, new_context25+from jinja2.runtime import Undefined, new_context, Context26 from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \27 TemplatesNotFound, TemplateRuntimeError28 from jinja2.utils import import_string, LRUCache, Markup, missing, \29 concat, consume, internalcode211 unmodified lines241 #: the class that is used for code generation. See242 #: :class:`~jinja2.compiler.CodeGenerator` for more information.243 code_generator_class = CodeGenerator244 245+ #: the context class thatis used for templates. See246+ #: :class:`~jinja2.runtime.Context` for more information.247+ context_class = Context248+249 def __init__(self,250 block_start_string=BLOCK_START_STRING,251 block_end_string=BLOCK_END_STRING,252 variable_start_string=VARIABLE_START_STRING,961 unmodified linesmodified:jinja2/runtime.py+2 added-1 removed
67 unmodified lines68 parent = dict(parent)69 for key, value in iteritems(locals):70 if key[:2] == 'l_' and value is not missing:71 parent[key[2:]] = value72- return Context(environment, parent, template_name, blocks)72+ return environment.context_class(environment, parent, template_name,73+ blocks)74 75 76 class TemplateReference(object):77 """The `self` in templates."""590 unmodified linesmodified:tests/test_api.py+13 added
15 unmodified lines16 from jinja2 import Environment, Undefined, DebugUndefined, \17 StrictUndefined, UndefinedError, meta, \18 is_undefined, Template, DictLoader, make_logging_undefined19 from jinja2.compiler import CodeGenerator20+from jinja2.runtime import Context21 from jinja2.utils import Cycler22 23 24 @pytest.mark.api287 unmodified lines312 313 env = CustomEnvironment()314 tmpl = env.from_string('{% set foo = "foo" %}{{ foo }}')315 assert tmpl.render() == 'bar'316+317+ def test_custom_context(self):318+ class CustomContext(Context):319+ def resolve(self, key):320+ return 'resolve-' + key321+322+ class CustomEnvironment(Environment):323+ context_class = CustomContext324+325+ env = CustomEnvironment()326+ tmpl = env.from_string('{{ foo }}')327+ assert tmpl.render() == 'resolve-foo'