Evaluation Context
The evaluation context (short eval context or eval ctx) is a new objectintroduced in Jinja 2.4 that makes it possible to activate and deactivatecompiled features at runtime.
Currently it is only used to enable and disable the automatic escaping butcan be used for extensions as well.
In previous Jinja versions filters and functions were marked asenvironment callables in order to check for the autoescape status from theenvironment. In new versions it’s encouraged to check the setting from theevaluation context instead.
Previous versions:
@environmentfilter
def filter(env, value):
result = do_something(value)
if env.autoescape:
result = Markup(result)
return result
In new versions you can either use a contextfilter()
and access theevaluation context from the actual context, or use aevalcontextfilter()
which directly passes the evaluation context tothe function:
@contextfilter
def filter(context, value):
result = do_something(value)
if context.eval_ctx.autoescape:
result = Markup(result)
return result
@evalcontextfilter
def filter(eval_ctx, value):
result = do_something(value)
if eval_ctx.autoescape:
result = Markup(result)
return result
The evaluation context must not be modified at runtime. Modificationsmust only happen with a nodes.EvalContextModifier
andnodes.ScopedEvalContextModifier
from an extension, not on theeval context object itself.