Skip to content
Myr

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

4 files+27 added−2 removed

modified:docs/api.rst+7 added
158 unmodified lines
159 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.162163 .. attribute:: context_class164165 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`.169170 .. automethod:: overlay([options])171172 .. method:: undefined([hint, obj, name, exc])173
635 unmodified lines
modified:jinja2/environment.py+5 added-1 removed
20 unmodified lines
21from jinja2.parser import Parser22from jinja2.nodes import EvalContext23from jinja2.optimizer import optimize24from jinja2.compiler import generate, CodeGenerator25from jinja2.runtime import Undefined, new_context25from jinja2.runtime import Undefined, new_context, Context26from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \27 TemplatesNotFound, TemplateRuntimeError28from jinja2.utils import import_string, LRUCache, Markup, missing, \29 concat, consume, internalcode
211 unmodified lines
241 #: the class that is used for code generation. See242 #: :class:`~jinja2.compiler.CodeGenerator` for more information.243 code_generator_class = CodeGenerator244245 #: the context class thatis used for templates. See246 #: :class:`~jinja2.runtime.Context` for more information.247 context_class = Context248249 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 lines
modified:jinja2/runtime.py+2 added-1 removed
67 unmodified lines
68 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)747576class TemplateReference(object):77 """The `self` in templates."""
590 unmodified lines
modified:tests/test_api.py+13 added
15 unmodified lines
16from jinja2 import Environment, Undefined, DebugUndefined, \17 StrictUndefined, UndefinedError, meta, \18 is_undefined, Template, DictLoader, make_logging_undefined19from jinja2.compiler import CodeGenerator20from jinja2.runtime import Context21from jinja2.utils import Cycler222324@pytest.mark.api
287 unmodified lines
312313 env = CustomEnvironment()314 tmpl = env.from_string('{% set foo = "foo" %}{{ foo }}')315 assert tmpl.render() == 'bar'316317 def test_custom_context(self):318 class CustomContext(Context):319 def resolve(self, key):320 return 'resolve-' + key321322 class CustomEnvironment(Environment):323 context_class = CustomContext324325 env = CustomEnvironment()326 tmpl = env.from_string('{{ foo }}')327 assert tmpl.render() == 'resolve-foo'