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])
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, \
#: :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,
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):
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
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'