MCPcopy Create free account
hub / github.com/nodejs/node / find_referenced_templates

Function find_referenced_templates

tools/inspector_protocol/jinja2/meta.py:60–106  ·  view source on GitHub ↗

Finds all the referenced templates from the AST. This will return an iterator over all the hardcoded template extensions, inclusions and imports. If dynamic inheritance or inclusion is used, `None` will be yielded. >>> from jinja2 import Environment, meta >>> env = Environment

(ast)

Source from the content-addressed store, hash-verified

58
59
60def find_referenced_templates(ast):
61 """Finds all the referenced templates from the AST. This will return an
62 iterator over all the hardcoded template extensions, inclusions and
63 imports. If dynamic inheritance or inclusion is used, `None` will be
64 yielded.
65
66 >>> from jinja2 import Environment, meta
67 >>> env = Environment()
68 >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')
69 >>> list(meta.find_referenced_templates(ast))
70 ['layout.html', None]
71
72 This function is useful for dependency tracking. For example if you want
73 to rebuild parts of the website after a layout template has changed.
74 """
75 for node in ast.find_all((nodes.Extends, nodes.FromImport, nodes.Import,
76 nodes.Include)):
77 if not isinstance(node.template, nodes.Const):
78 # a tuple with some non consts in there
79 if isinstance(node.template, (nodes.Tuple, nodes.List)):
80 for template_name in node.template.items:
81 # something const, only yield the strings and ignore
82 # non-string consts that really just make no sense
83 if isinstance(template_name, nodes.Const):
84 if isinstance(template_name.value, string_types):
85 yield template_name.value
86 # something dynamic in there
87 else:
88 yield None
89 # something dynamic we don't know about here
90 else:
91 yield None
92 continue
93 # constant is a basestring, direct template name
94 if isinstance(node.template.value, string_types):
95 yield node.template.value
96 # a tuple or list (latter *should* not happen) made of consts,
97 # yield the consts that are strings. We could warn here for
98 # non string values
99 elif isinstance(node, nodes.Include) and \
100 isinstance(node.template.value, (tuple, list)):
101 for template_name in node.template.value:
102 if isinstance(template_name, string_types):
103 yield template_name
104 # something else we don't care about, we could warn here
105 else:
106 yield None

Callers

nothing calls this directly

Calls 1

find_allMethod · 0.45

Tested by

no test coverage detected