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

Function find_referenced_templates

deps/v8/third_party/jinja2/meta.py:61–111  ·  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: nodes.Template)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

find_allMethod · 0.45

Tested by

no test coverage detected