class documentation

class Context: (source)

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 :attr:`parent` **must not** happen and modifications on :attr:`vars` are allowed from generated template code only. Template filters and global functions marked as :func:`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 :meth:`resolve` method that doesn't fail with a `KeyError` but returns an :class:`Undefined` object for missing variables.

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 __repr__ Undocumented
Method call Call the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable has :func:`pass_context` or :func:`pass_environment`.
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 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 :class:`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.
Method super Render a parent block.
Class Variable items Undocumented
Class Variable keys Undocumented
Class Variable values Undocumented
Instance Variable blocks Undocumented
Instance Variable environment Undocumented
Instance Variable eval_ctx Undocumented
Instance Variable exported_vars Undocumented
Instance Variable globals_keys Undocumented
Instance Variable name Undocumented
Instance Variable parent Undocumented
Instance Variable vars Undocumented
def __contains__(self, name): (source)

Undocumented

Parameters
name:strUndocumented
Returns
boolUndocumented
def __getitem__(self, key): (source)

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): (source)

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 __repr__(self): (source)

Undocumented

Returns
strUndocumented
@internalcode
def call(__self, __obj, *args, **kwargs): (source)

Call the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable has :func:`pass_context` or :func:`pass_environment`.

Parameters
__selfUndocumented
__obj:t.CallableUndocumented
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
t.Union[t.Any, Undefined]Undocumented
def derived(self, locals=None): (source)

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 get(self, key, default=None): (source)

Look up a variable by name, or return a default if the key is not found. :param key: The variable name to look up. :param default: The value to return if the key is not found.

Parameters
key:strUndocumented
default:t.AnyUndocumented
Returns
t.AnyUndocumented
def get_all(self): (source)

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): (source)

Get a new dict with the exported variables.

Returns
t.Dict[str, t.Any]Undocumented
def resolve(self, key): (source)

Look up a variable by name, or return an :class:`Undefined` object if the key is not found. If you need to add custom behavior, override :meth:`resolve_or_missing`, not this method. The various lookup functions use that method, not this one. :param key: The variable name to look up.

Parameters
key:strUndocumented
Returns
t.Union[t.Any, Undefined]Undocumented
def resolve_or_missing(self, key): (source)

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. :meth:`resolve`, :meth:`get`, and :meth:`__getitem__` use this method. Don't call this method directly. :param key: The variable name to look up.

Parameters
key:strUndocumented
Returns
t.AnyUndocumented
def super(self, name, current): (source)

Render a parent block.

Parameters
name:strUndocumented
current:t.Callable[[Context], t.Iterator[str]]Undocumented
Returns
t.Union[BlockReference, Undefined]Undocumented
items = (source)

Undocumented

keys = (source)

Undocumented

values = (source)

Undocumented

blocks = (source)

Undocumented

environment: Environment = (source)

Undocumented

eval_ctx = (source)

Undocumented

exported_vars: t.Set[str] = (source)

Undocumented

globals_keys = (source)

Undocumented

name = (source)

Undocumented

parent = (source)

Undocumented

Undocumented