python - Jinja2 find_undeclared_variables ignores globals? -
i want find out variables of jinja2 template not covered globals. load template source, parse , feed result meta.find_undeclared_variables
. no matter in global dictionary of environment full list of variables template. how make operation recognize globals in environment , in template , return list of variables not covered them.
the sample below creates environment, renders template show global variables indeed read , calls meta.find_undeclared_variables
show result.
from jinja2 import environment, meta, functionloader, prefixloader def load_mapping(name): return 'mapping %s {{version}} {{docid}}' % name def load_link(name): return 'link %s {{version}} {{docid}}' % name loader = prefixloader({ 'link': functionloader(load_link), 'map': functionloader(load_mapping) }) env = environment(loader=loader) globals = {'version': '1.0'} env.globals.update(globals) print env.get_template('map/test').render(docid='asdf') tsrc = env.loader.get_source(env, 'link/test') parsed = env.parse(tsrc) print meta.find_undeclared_variables(parsed)
the code prints:
mapping test 1.0 asdf set(['version', 'docid'])
with version
being global change code docid
returned.
i use python 2.7.6 , jinja 2.7.3.
jinja_globals = env.globals.keys() print meta.find_undeclared_variables(parsed) - set(jinja_globals)
Comments
Post a Comment