Context Class

Inheritance diagram of Context

class Context(environment, parent, name, blocks)

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 contextfunction()s 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.

Methods

__init__(environment, parent, name, blocks)
call(_Context__self, _Context__obj, *args, ...) Call the callable with the arguments and keyword arguments
derived([locals]) Internal helper function to create a derived context.
get(key[, default]) Returns an item from the template context, if it doesn’t exist
get_all() Return a copy of the complete context as dict including the
get_exported() Get a new dict with the exported variables.
items(() -> list of D’s (key, value) pairs, ...)
iteritems(() -> an iterator over the (key, ...)
iterkeys(() -> an iterator over the keys of D)
itervalues(...)
keys(() -> list of D’s keys)
resolve(key) Looks up a variable like __getitem__ or get but returns an
super(name, current) Render a parent block.
values(() -> list of D’s values)

Attributes

blocks
environment
eval_ctx
exported_vars
name
parent
vars

Descriptions

class Context

Method details

__init__(environment, parent, name, blocks)
call(_Context__self, _Context__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 is a contextfunction() or environmentfunction().

derived(locals=None)

Internal helper function to create a derived context.

get(key, default=None)

Returns an item from the template context, if it doesn’t exist default is returned.

get_all()

Return a copy of the complete context as dict including the exported variables.

get_exported()

Get a new dict with the exported variables.

items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
resolve(key)

Looks up a variable like __getitem__ or get but returns an Undefined object with the name of the name looked up.

super(name, current)

Render a parent block.

values() → list of D's values

Attribute details

blocks
environment
eval_ctx
exported_vars
name
parent
vars

Table Of Contents

Previous topic

BlockReference Class

Next topic

DebugUndefined Class