jinja2.environment.Context

class jinja2.environment.Context(environment, parent, name, blocks)[source]

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 provided but inject the active context or environment as first argument if the callable is a contextfunction() or environmentfunction().
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 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, ...)
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 Undefined object with the name of the name looked up.
super(name, current) Render a parent block.
values(() -> list of D’s values)