class Context:
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 |
pass_context
or
pass_environment
.Parameters | |
__self | Undocumented |
__obj:t.Callable | Undocumented |
*args:t.Any | Undocumented |
**kwargs:t.Any | Undocumented |
Returns | |
t.Union[ | Undocumented |
Parameters | |
key:str | The variable name to look up. |
default:t.Any | The value to return if the key is not found. |
Returns | |
t.Any | Undocumented |
Returns | |
t.Dict[ | Undocumented |
Returns | |
t.Dict[ | Undocumented |
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:str | The variable name to look up. |
Returns | |
t.Union[ | Undocumented |
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:str | The variable name to look up. |
Returns | |
t.Any | Undocumented |
Parameters | |
key:str | Undocumented |
Returns | |
t.Any | Undocumented |
Undocumented
Parameters | |
environment:Environment | Undocumented |
parent:t.Dict[ | Undocumented |
name:t.Optional[ | Undocumented |
blocks:t.Dict[ | Undocumented |
globals:t.Optional[ | Undocumented |
Parameters | |
locals:t.Optional[ | Undocumented |
Returns | |
Context | Undocumented |
Parameters | |
name:str | Undocumented |
current:t.Callable[ | Undocumented |
Returns | |
t.Union[ | Undocumented |