class documentation

class Context:

View In Hierarchy

The template context holds the variables of a template. It stores the values passed to the template and also the names the template exports. Creating instances is neither supported nor useful as it's created automatically at various stages of the template evaluation and should not be created by hand.

The context is immutable. Modifications on parent must not happen and modifications on vars are allowed from generated template code only. Template filters and global functions marked as pass_context get the active context passed as first argument and are allowed to access the context read-only.

The template context supports read only dict operations (get, keys, values, items, iterkeys, itervalues, iteritems, __getitem__, __contains__). Additionally there is a resolve method that doesn't fail with a KeyError but returns an Undefined object for missing variables.

Method call No summary
Method get Look up a variable by name, or return a default if the key is not found.
Method get​_all Return the complete context as dict including the exported variables. For optimizations reasons this might not return an actual copy so be careful with using it.
Method get​_exported Get a new dict with the exported variables.
Method resolve Look up a variable by name, or return an Undefined object if the key is not found.
Method resolve​_or​_missing Look up a variable by name, or return a missing sentinel if the key is not found.
Instance Variable blocks Undocumented
Instance Variable environment Undocumented
Instance Variable eval​_ctx Undocumented
Instance Variable exported​_vars Undocumented
Instance Variable name Undocumented
Instance Variable parent Undocumented
Instance Variable vars Undocumented
Method __contains__ Undocumented
Method __getitem__ Look up a variable by name with [] syntax, or raise a KeyError if the key is not found.
Method __init__ Undocumented
Method __init​_subclass__ Undocumented
Method __repr__ Undocumented
Method derived Internal helper function to create a derived context. This is used in situations where the system needs a new context in the same template that is independent.
Method super Render a parent block.
Class Variable ​_legacy​_resolve​_mode Undocumented
Class Variable items Undocumented
Class Variable keys Undocumented
Class Variable values Undocumented
Instance Variable globals​_keys Undocumented
@internalcode
def call(__self, __obj, *args, **kwargs):
Call the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable has pass_context or pass_environment.
Parameters
__selfUndocumented
__obj:t.CallableUndocumented
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
t.Union[t.Any, Undefined]Undocumented
def get(self, key, default=None):
Look up a variable by name, or return a default if the key is not found.
Parameters
key:strThe variable name to look up.
default:t.AnyThe value to return if the key is not found.
Returns
t.AnyUndocumented
def get_all(self):
Return the complete context as dict including the exported variables. For optimizations reasons this might not return an actual copy so be careful with using it.
Returns
t.Dict[str, t.Any]Undocumented
def get_exported(self):
Get a new dict with the exported variables.
Returns
t.Dict[str, t.Any]Undocumented
def resolve(self, key):

Look up a variable by name, or return an Undefined object if the key is not found.

If you need to add custom behavior, override resolve_or_missing, not this method. The various lookup functions use that method, not this one.

Parameters
key:strThe variable name to look up.
Returns
t.Union[t.Any, Undefined]Undocumented
def resolve_or_missing(self, key):

Look up a variable by name, or return a missing sentinel if the key is not found.

Override this method to add custom lookup behavior. resolve, get, and __getitem__ use this method. Don't call this method directly.

Parameters
key:strThe variable name to look up.
Returns
t.AnyUndocumented
blocks =

Undocumented

environment: Environment =

Undocumented

eval_ctx =

Undocumented

exported_vars: t.Set[str] =

Undocumented

name =

Undocumented

parent =

Undocumented

vars: t.Dict[str, t.Any] =

Undocumented

def __contains__(self, name):

Undocumented

Parameters
name:strUndocumented
Returns
boolUndocumented
def __getitem__(self, key):
Look up a variable by name with [] syntax, or raise a KeyError if the key is not found.
Parameters
key:strUndocumented
Returns
t.AnyUndocumented
def __init__(self, environment, parent, name, blocks, globals=None):

Undocumented

Parameters
environment:EnvironmentUndocumented
parent:t.Dict[str, t.Any]Undocumented
name:t.Optional[str]Undocumented
blocks:t.Dict[str, t.Callable[[Context], t.Iterator[str]]]Undocumented
globals:t.Optional[t.MutableMapping[str, t.Any]]Undocumented
def __init_subclass__(cls):

Undocumented

def __repr__(self):

Undocumented

Returns
strUndocumented
def derived(self, locals=None):
Internal helper function to create a derived context. This is used in situations where the system needs a new context in the same template that is independent.
Parameters
locals:t.Optional[t.Dict[str, t.Any]]Undocumented
Returns
ContextUndocumented
def super(self, name, current):
Render a parent block.
Parameters
name:strUndocumented
current:t.Callable[[Context], t.Iterator[str]]Undocumented
Returns
t.Union[BlockReference, Undefined]Undocumented
_legacy_resolve_mode: t.ClassVar[bool] =

Undocumented

items =

Undocumented

keys =

Undocumented

values =

Undocumented

globals_keys =

Undocumented