I have a usecase where I want to get a list of unreferenced variables in a jinja2 template. I found some explanations on stackoverflow on how to do this, but not of these exaples use a file as a template, and I am very, very stuck
Here is my code. Lines 8 and 9 can be omitted, ofc.
import jinja2
from jinja2 import meta
env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="./"))
template_file = "testfile.txt"
template = env.get_template(template_file)
data = dict(foo='foo', bar='bar')
print(template.render(data))
data = dict(foo='foo')
test = env.parse(data, template)
print(meta.find_undeclared_variables(test))
here is the content of 'testfile.txt'
this should print foo:{{foo}}
this should print bar:{{bar}}
here's my output
this should print foo:foo
this should print bar:bar
set()
What I would like to get as output is the string 'bar' in the set like below, as 'bar' is not referenced in the 'data' dict.
this should print foo:foo
this should print bar:bar
set('bar')
any help solving this would be greatly appreciated