Skip to content

Template Analysis

TemplateAnalysis

TemplateAnalysis(
    *,
    variables: Refs,
    local_variables: Refs,
    global_variables: Refs,
    failed_visits: NameRefs,
    unloadable_partials: NameRefs,
    filters: NameRefs,
    tags: NameRefs
)

The result of analyzing a template using BoundTemplate.analyze.

Each of the following properties is a dictionary mapping variable, tag or filter names to a list of tuples. Each tuple holds the location of a reference to the name as (template name, line number). If a name is referenced multiple times, it will appear multiple times in the list. If a name is referenced before it is "assigned", it will appear in local_variables and global_variables.

ATTRIBUTE DESCRIPTION
variables

All referenced variables, whether they are in scope or not. Including references to names such as forloop from the for tag.

TYPE: Refs

local_variables

Template variables that are added to the template local scope, whether they are subsequently used or not.

TYPE: Refs

global_variables

Template variables that, on the given line number and "file", are out of scope or are assumed to be "global". That is, expected to be included by the application developer rather than a template author.

TYPE: Refs

failed_visits

Names of AST Node and Expression objects that could not be visited, probably because they do not implement a children method.

TYPE: NameRefs

unloadable_partials

Names or identifiers of partial templates that could not be loaded. This will be empty if follow_partials is False.

TYPE: NameRefs

filters

All filters found during static analysis.

TYPE: NameRefs

tags

All tags found during static analysis.

ReferencedVariable

ReferencedVariable(_: object)

Bases: str

A str subclass for variables found during static analysis.

parts property

parts: IdentifierTuple

A tuple representation of the variable's parts.

parts might contain nested tuples for nested variables. For example, the variable some[foo.bar[a.b]].other as a tuple would look like this:

("some", ("foo", "bar", ("a", "b")), "other.thing")

Refs module-attribute

Refs = Dict[ReferencedVariable, List[Location]]

A mapping of template variables to their (template_name, line_number) locations.

NameRefs module-attribute

NameRefs = Dict[str, List[Location]]

A mapping of template, tag or filter names to (template_name, lineno) locations.

ContextualTemplateAnalysis

ContextualTemplateAnalysis(
    *,
    all_variables: Dict[str, int],
    local_variables: Dict[str, int],
    undefined_variables: Dict[str, int],
    filters: Dict[str, int]
)

The result of analyzing a template using BoundTemplate.analyze_with_context.

Each of the following properties is a dictionary mapping variable or filter names to the number of times that variable was referenced.

ATTRIBUTE DESCRIPTION
all_variables

All variables references along a path through the template's syntax tree.

TYPE: Dict[str, int]

local_variables

The names of variables assigned using the built-in assign capture, increment or decrement tags, or any custom tag that uses Context.assign().

TYPE: Dict[str, int]

undefined_variables

The names of variables that could not be resolved. If a name is referenced before it is assigned, it will appear in undefined and assigns.

TYPE: Dict[str, int]

filters

Names of filters found during contextual analysis.

TYPE: Dict[str, int]

TagAnalysis

TagAnalysis(
    *,
    env: Environment,
    name: str,
    tokens: List[Token],
    inner_tags: Optional[InnerTagMap] = None
)

The result of analyzing a template's tags with Environment.analyze_tags().

Each of the following properties maps tag names to a list of their locations. Locations are (template_name, line_number) tuples.

Note that raw tags are not included at all. The lexer converts them to text tokens before we get a chance to analyze them.

Also be aware that reported unexpected_tags don't handle the possibility of an "inner" tag appearing in a partial template (using {% include %}), where appropriate enclosing block tags are in the parent template.

ATTRIBUTE DESCRIPTION
all_tags

A mapping of all tags to their locations. Includes "end", "inner" and unknown tags.

tags

A mapping of tag names to their locations. Excludes "end" and "inner" tags.

unclosed_tags

Block tags that don't have a matching "end" tag.

unexpected_tags

Inner tags that are not properly enclosed by appropriate block tags. For example, an {% else %} that is not enclosed by a {% for %} or {% unless %} block.

unknown_tags

Tags that are unknown to the environment.