Get list of variables from Jinja2 template (parent and child)
<p>I'm trying to get a list of variables from a Jinja2 template.</p>

<p>test1.j2:</p>

<pre><code>some-non-relevant-content
{{var1}}
{% include 'test2.j2' %}
</code></pre>

<p>test2.j2:</p>

<pre><code>another-text
{{var2}}
</code></pre>

<p>I can get variables from test1 easily:</p>

<pre><code>env = Environment(loader=FileSystemLoader(searchpath='./Templates'))
src_t = env.loader.get_source(env, 'test1.j2')[0]
parsed_t = env.parse(source=src_t)
t_vars = meta.find_undeclared_variables(ast=parsed_t)
</code></pre>

<p>Problem is, I can only get variables from the parent template with get_source.
Obviously, I can not feed class template object to parse method as well.</p>

<p>Is there any way to build the full list? {'var1', 'var2'} in my case.
Ideally by using Jinja2 API. Minimum custom code.</p>

<p>Found a way to code that without a big pain.
meta.find_referenced_templates helps to load all child templates when applied recursively. When done, it's trivial to get variables from all templates in a single list.</p>

python python-3.x jinja2