Skip to content

Static template analysis

Instances of Template, as returned by parse(), from_string() and get_template(), include several methods for inspecting a template's variable, tag a filter usage, without rendering the template.

By default, all of these methods will try to load and analyze included, rendered and extended templates too. Set the include_partials keyword only argument to False to disable automatic loading and analysis of partial/parent templates.

Variables

variables() and variables_async() return a list of distinct variables used in the template, without path segments. The list will include variables that are local to the template, like those crated with {% assign %} and {% capture %}, or are in scope from {% for %} tags.

from liquid2 import parse

source = """\
Hello, {{ you }}!
{% assign x = 'foo' | upcase %}

{% for ch in x %}
    - {{ ch }}
{% endfor %}

Goodbye, {{ you.first_name | capitalize }} {{ you.last_name }}
Goodbye, {{ you.first_name }} {{ you.last_name }}
"""

template = parse(source)
print(template.variables())
output
['you', 'x', 'ch']

Variable paths

variable_paths() and variable_paths_async() return a list of variables used in the template, including all path segments. The list will include variables that are local to the template, like those crated with {% assign %} and {% capture %}, or are in scope from {% for %} tags.

# ... continued from above

print(template.variable_paths())
output
['you.first_name', 'you', 'you.last_name', 'x', 'ch']

Variable segments

variable_segments() and variable_segments_async() return a list of variables used in the template, each as a list of segments. The list will include variables that are local to the template, like those crated with {% assign %} and {% capture %}, or are in scope from {% for %} tags.

# ... continued from above

print(template.variable_segments())
output
[
    ["you", "last_name"],
    ["you"],
    ["you", "first_name"],
    ["ch"],
    ["x"],
]

Global variables

global_variables() and global_variables_async() return a list of variables used in the template, without path segments and excluding variables that are local to the template.

# ... continued from above

print(template.global_variables())
output
['you']

Global variable paths

global_variable_paths() and global_variable_paths_async() return a list of variables used in the template, with path segments and excluding variables that are local to the template.

# ... continued from above

print(template.global_variable_paths())
output
['you', 'you.first_name', 'you.last_name']

Global variable segments

global_variable_segments() and global_variable_segments_async() return a list of variables used in the template, each as a list of segments, excluding variables that are local to the template.

# ... continued from above

print(template.global_variable_segments())
output
[
    ['you', 'last_name'],
    ['you', 'first_name'],
    ['you'],
]

Filter names

filter_names() and filter_names_async() return names of filters used in the template.

# ... continued from above

print(template.filter_names())
output
['upcase', 'capitalize']

Tag names

tag_names() and tag_names_async() return the names of tags used in the template.

# ... continued from above

print(template.tag_names())
output
['assign', 'for']

Variable, tag and filter locations

analyze() and analyze_async() return an instance of TemplateAnalysis. It contains all of the information provided by the methods described above, but includes the location of each variable, tag and filter, each of which can appear many times across many templates.

Using the same example template show at the top of this page, pretty printing template.analyze() gives us the following output.

TemplateAnalysis(variables={'ch': [Variable(segments=['ch'],
                                            span=Span(template_name='',
                                                      start=78,
                                                      end=80))],
                            'x': [Variable(segments=['x'],
                                           span=Span(template_name='',
                                                     start=64,
                                                     end=65))],
                            'you': [Variable(segments=['you'],
                                             span=Span(template_name='',
                                                       start=10,
                                                       end=13)),
                                    Variable(segments=['you', 'first_name'],
                                             span=Span(template_name='',
                                                       start=110,
                                                       end=124)),
                                    Variable(segments=['you', 'last_name'],
                                             span=Span(template_name='',
                                                       start=144,
                                                       end=157)),
                                    Variable(segments=['you', 'first_name'],
                                             span=Span(template_name='',
                                                       start=173,
                                                       end=187)),
                                    Variable(segments=['you', 'last_name'],
                                             span=Span(template_name='',
                                                       start=194,
                                                       end=207))]},
                 globals={'you': [Variable(segments=['you'],
                                           span=Span(template_name='',
                                                     start=10,
                                                     end=13)),
                                  Variable(segments=['you', 'first_name'],
                                           span=Span(template_name='',
                                                     start=110,
                                                     end=124)),
                                  Variable(segments=['you', 'last_name'],
                                           span=Span(template_name='',
                                                     start=144,
                                                     end=157)),
                                  Variable(segments=['you', 'first_name'],
                                           span=Span(template_name='',
                                                     start=173,
                                                     end=187)),
                                  Variable(segments=['you', 'last_name'],
                                           span=Span(template_name='',
                                                     start=194,
                                                     end=207))]},
                 locals={'x': [Variable(segments=['x'],
                                        span=Span(template_name='',
                                                  start=28,
                                                  end=29))]},
                 filters={'capitalize': [Span(template_name='',
                                              start=127,
                                              end=137)],
                          'upcase': [Span(template_name='', start=40, end=46)]},
                 tags={'assign': [Span(template_name='', start=18, end=49)],
                       'for': [Span(template_name='', start=51, end=68)]})