且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取Jinja 2模板中所有变量的列表

更新时间:2023-11-02 17:04:46

由于没有人回答问题并且我找到了答案

Since no one has answered the question and I found the answer

from jinja2 import Environment, PackageLoader, meta
env = Environment(loader=PackageLoader('gummi', 'templates'))
template_source = env.loader.get_source(env, 'page_content.html')
parsed_content = env.parse(template_source)
meta.find_undeclared_variables(parsed_content)

这将产生未声明变量的列表,因为它不是在运行时执行的,它将产生所有变量的列表.

This will yield list of undeclared variables since this is not executed at run time, it will yield list of all variables.

注意:这将产生使用includeextends包含的html文件.

Note: This will yield html files which are included using include and extends.