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

发布于 2024-12-18 01:14:17 字数 292 浏览 4 评论 0原文

我正在尝试获取模板中所有变量和块的列表。我不想创建自己的解析器来查找变量。我尝试使用以下代码片段。

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('gummi', 'templates'))
template = env.get_template('chat.html')

template.blocks 是字典,其中键是块,如何获取块内的所有变量?

I am trying to get list of all variables and blocks in a template. I don't want to create my own parser to find variables. I tried using following snippet.

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('gummi', 'templates'))
template = env.get_template('chat.html')

template.blocks is dict where keys are blocks, how can I get all variables inside the blocks ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

口干舌燥 2024-12-25 01:14:17

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

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)

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

注意:这将生成使用 includeextends 包含的 html 文件。

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.

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

月棠 2024-12-25 01:14:17

我有同样的需求,并且编写了一个名为 jinja2schema 的工具。它提供了一种启发式算法,用于从 Jinja2 模板推断类型,也可用于获取所有模板变量(包括嵌套变量)的列表。

这是这样做的一个简短示例:

>>> import jinja2
>>> import jinja2schema
>>>
>>> template = '''
... {{ x }}
... {% for y in ys %}
...     {{ y.nested_field_1 }}
...     {{ y.nested_field_2 }}
... {% endfor %}
... '''
>>> variables = jinja2schema.infer(template)
>>>
>>> variables
{'x': <scalar>,
 'ys': [{'nested_field_1': <scalar>, 'nested_field_2': <scalar>}]}
>>>
>>> variables.keys()
['x', 'ys']
>>> variables['ys'].item.keys()
['nested_field_2', 'nested_field_1']

I had the same need and I've written a tool called jinja2schema. It provides a heuristic algorithm for inferring types from Jinja2 templates and can also be used for getting a list of all template variables, including nested ones.

Here is a short example of doing that:

>>> import jinja2
>>> import jinja2schema
>>>
>>> template = '''
... {{ x }}
... {% for y in ys %}
...     {{ y.nested_field_1 }}
...     {{ y.nested_field_2 }}
... {% endfor %}
... '''
>>> variables = jinja2schema.infer(template)
>>>
>>> variables
{'x': <scalar>,
 'ys': [{'nested_field_1': <scalar>, 'nested_field_2': <scalar>}]}
>>>
>>> variables.keys()
['x', 'ys']
>>> variables['ys'].item.keys()
['nested_field_2', 'nested_field_1']
只是在用心讲痛 2024-12-25 01:14:17

对于我的鹈鹕主题,我创建了一个工具来分析模板文件中的所有 jinja 变量。

我分享我的代码

该脚本从模板文件中存在的所有变量生成示例配置,并从我的官方 pelicanconf.py 获取变量

从模板文件中提取所有变量的函数

def get_variables(filename):
    env = Environment(loader=FileSystemLoader('templates'))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)

完整的脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# use:
# generate_pelicanconf-sample.py my_official_blog/pelicanconf.py

import sys
import imp
import os

from jinja2 import Environment, FileSystemLoader, meta


# Search all template files
def list_html_templates():
    dirList = os.listdir('templates')

    return dirList


# get all variable in template file
def get_variables(filename):
    env = Environment(loader=FileSystemLoader('templates'))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)

    return meta.find_undeclared_variables(parsed_content)


# Check if the pelicanconf.py is in param
if len(sys.argv) != 2:
    print("Please indicate the pelicanconf.py file")
    sys.exit()

# Get all vars from templates files
all_vars = set()
files = list_html_templates()
for fname in files:
    variables = get_variables(fname)
    for var in variables:
        if var.isupper():
            all_vars.add(var)

m = imp.load_source('pelicanconf', sys.argv[1])

# Show pelicanconf.py vars content
for var in all_vars:
    varname = 'm.%s' % var
    if var in m.__dict__:
        print ("%s = %s" % (var, repr(m.__dict__[var])))


    return meta.find_undeclared_variables(parsed_content)

该程序的示例结果

LINKS = ((u'Home', u'/'), (u'archives', u'/archives.html'), (u'tags', u'/tags.html'), (u'A propos', u'http://bruno.adele.im'))
SITESUBTITLE = u'Une famille compl\xe8tement 633<'
DEFAULT_LANG = u'fr'
SITEURL = u'http://blog.jesuislibre.org'
AUTHOR = u'Bruno Adel\xe9'
SITENAME = u'Famille de geeks'
SOCIAL = ((u'adele', u'http://adele.im'), (u'feed', u'http://feeds.feedburner.com/FamilleDeGeek'), (u'twitter', u'http://twitter.com/jesuislibre.org'), (u'google+', u'https://plus.google.com/100723270029692582967'), (u'blog', u'http://blog.jesuislibre.org'), (u'facebook', u'http://www.facebook.com/bruno.adele'), (u'flickr', u'http://www.flickr.com/photos/b_adele'), (u'linkedin', u'http://fr.linkedin.com/in/brunoadele'))
FEED_DOMAIN = u'http://blog.jesuislibre.org'
FEED_ALL_ATOM = u'feed.atom'
DISQUS_SITENAME = u'blogdejesuislibreorg'
DEFAULT_PAGINATION = 10
GITHUB_BLOG_SITE = u'https://github.com/badele/blog.jesuislibre.org'

有关此脚本的更多详细信息,请参阅 https://github.com /badele/pelican-theme-jesuislibre

For my pelican theme, i have created a tools for analyse all jinja variables in my templates files.

I share my code

This script generate a sample configuration from all variables exists in template files and get a variables from my official pelicanconf.py

The function that extract all variables from template file

def get_variables(filename):
    env = Environment(loader=FileSystemLoader('templates'))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)

The complete script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# use:
# generate_pelicanconf-sample.py my_official_blog/pelicanconf.py

import sys
import imp
import os

from jinja2 import Environment, FileSystemLoader, meta


# Search all template files
def list_html_templates():
    dirList = os.listdir('templates')

    return dirList


# get all variable in template file
def get_variables(filename):
    env = Environment(loader=FileSystemLoader('templates'))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)

    return meta.find_undeclared_variables(parsed_content)


# Check if the pelicanconf.py is in param
if len(sys.argv) != 2:
    print("Please indicate the pelicanconf.py file")
    sys.exit()

# Get all vars from templates files
all_vars = set()
files = list_html_templates()
for fname in files:
    variables = get_variables(fname)
    for var in variables:
        if var.isupper():
            all_vars.add(var)

m = imp.load_source('pelicanconf', sys.argv[1])

# Show pelicanconf.py vars content
for var in all_vars:
    varname = 'm.%s' % var
    if var in m.__dict__:
        print ("%s = %s" % (var, repr(m.__dict__[var])))


    return meta.find_undeclared_variables(parsed_content)

The sample result of this program

LINKS = ((u'Home', u'/'), (u'archives', u'/archives.html'), (u'tags', u'/tags.html'), (u'A propos', u'http://bruno.adele.im'))
SITESUBTITLE = u'Une famille compl\xe8tement 633<'
DEFAULT_LANG = u'fr'
SITEURL = u'http://blog.jesuislibre.org'
AUTHOR = u'Bruno Adel\xe9'
SITENAME = u'Famille de geeks'
SOCIAL = ((u'adele', u'http://adele.im'), (u'feed', u'http://feeds.feedburner.com/FamilleDeGeek'), (u'twitter', u'http://twitter.com/jesuislibre.org'), (u'google+', u'https://plus.google.com/100723270029692582967'), (u'blog', u'http://blog.jesuislibre.org'), (u'facebook', u'http://www.facebook.com/bruno.adele'), (u'flickr', u'http://www.flickr.com/photos/b_adele'), (u'linkedin', u'http://fr.linkedin.com/in/brunoadele'))
FEED_DOMAIN = u'http://blog.jesuislibre.org'
FEED_ALL_ATOM = u'feed.atom'
DISQUS_SITENAME = u'blogdejesuislibreorg'
DEFAULT_PAGINATION = 10
GITHUB_BLOG_SITE = u'https://github.com/badele/blog.jesuislibre.org'

For more détail of this script see https://github.com/badele/pelican-theme-jesuislibre

〆凄凉。 2024-12-25 01:14:17

对于那些想要找出所有变量而不仅仅是需要从外部设置的变量的人来说。可以执行以下操作:

from jinja2 import Environment, meta
from jinja2.nodes import Name
content='''
{% set y = 1%}
show y: {{ y }}
{% if x|default(2) == 1%}
{% endif %}
{{ z|default(3) }}
{{ w }}
{% set y2 = y3|default(6)%}
we do not use y2 at all
{% for _i in mylist %}
item is {{ _i }}
{% endfor %}
{{ dict1.x }}
{{ dict1.x2.y.z }}
'''
env = Environment()
parsed_content = env.parse(content)
print("undeclared=", meta.find_undeclared_variables(parsed_content))

vars = set()
vars_internal = set()
for name in parsed_content.find_all(Name):
    if name.ctx == 'load':
        vars.add(name.name)
    else:
        vars_internal.add(name.name)
print(f"{vars=}\n{vars_internal=}")
# we can see vars also contain the value that has been used
# y2 does not show up in vars because it never gets used
print(f"vars assigned but never used: {vars_internal.difference(vars)}")
print(f"vars need to be assigned from outside: {vars.difference(vars_internal)}")
print(f"vars only used internally: {vars_internal}")

输出是:

undeclared= {'dict1', 'x', 'z', 'mylist', 'y3', 'w'}
vars={'_i', 'dict1', 'x', 'z', 'mylist', 'y', 'y3', 'w'}
vars_internal={'_i', 'y', 'y2'}
vars assigned but never used: {'y2'}
vars need to be assigned from outside: {'dict1', 'x', 'z', 'mylist', 'y3', 'w'}
vars only used internally: {'_i', 'y', 'y2'}

有人正在询问字段,它也可以轻松存档:

# to obtain the nested field:
def recurse_getattr(g: Getattr):
    if isinstance(g.node, Getattr):
        return recurse_getattr(g.node) + "." + g.attr
    return g.node.name + "." + g.attr

all_fields = set()
for g in parsed_content.find_all(Getattr):
    all_fields.add(recurse_getattr(g))
print(all_fields)
# will output {'dict1.x2.y', 'dict1.x', 'dict1.x2', 'dict1.x2.y.z'}

for ones want to find out all variable not just the variable need to be set from outside. one can do following:

from jinja2 import Environment, meta
from jinja2.nodes import Name
content='''
{% set y = 1%}
show y: {{ y }}
{% if x|default(2) == 1%}
{% endif %}
{{ z|default(3) }}
{{ w }}
{% set y2 = y3|default(6)%}
we do not use y2 at all
{% for _i in mylist %}
item is {{ _i }}
{% endfor %}
{{ dict1.x }}
{{ dict1.x2.y.z }}
'''
env = Environment()
parsed_content = env.parse(content)
print("undeclared=", meta.find_undeclared_variables(parsed_content))

vars = set()
vars_internal = set()
for name in parsed_content.find_all(Name):
    if name.ctx == 'load':
        vars.add(name.name)
    else:
        vars_internal.add(name.name)
print(f"{vars=}\n{vars_internal=}")
# we can see vars also contain the value that has been used
# y2 does not show up in vars because it never gets used
print(f"vars assigned but never used: {vars_internal.difference(vars)}")
print(f"vars need to be assigned from outside: {vars.difference(vars_internal)}")
print(f"vars only used internally: {vars_internal}")

output is:

undeclared= {'dict1', 'x', 'z', 'mylist', 'y3', 'w'}
vars={'_i', 'dict1', 'x', 'z', 'mylist', 'y', 'y3', 'w'}
vars_internal={'_i', 'y', 'y2'}
vars assigned but never used: {'y2'}
vars need to be assigned from outside: {'dict1', 'x', 'z', 'mylist', 'y3', 'w'}
vars only used internally: {'_i', 'y', 'y2'}

Someone is asking for the fields, it can also easily archived:

# to obtain the nested field:
def recurse_getattr(g: Getattr):
    if isinstance(g.node, Getattr):
        return recurse_getattr(g.node) + "." + g.attr
    return g.node.name + "." + g.attr

all_fields = set()
for g in parsed_content.find_all(Getattr):
    all_fields.add(recurse_getattr(g))
print(all_fields)
# will output {'dict1.x2.y', 'dict1.x', 'dict1.x2', 'dict1.x2.y.z'}
栖竹 2024-12-25 01:14:17

对我来说 jinja2.meta.find_undeclared_variables(parsed_content) 不太合适,因为它不提供嵌套变量。

jinja2schema 工具对于简单的场景来说还不错,但是对于所有循环和其他 jinja2 黑暗力量,它会失败并出现错误。

我尝试过 jinja2 数据结构,并且能够获取所有变量,包括嵌套变量。对于我的用例来说,这已经足够了。也许这对其他人也有帮助:)

这是代码:

from jinja2 import Environment, FileSystemLoader, nodes


def get_variables(path, filename):
    template_variables = set()
    env = Environment(loader=FileSystemLoader(searchpath=path))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)
    if parsed_content.body and hasattr(parsed_content.body[0], 'nodes'):
        for variable in parsed_content.body[0].nodes:
            if type(variable) is nodes.Name or type(variable) is nodes.Getattr:
                parsed_variable = parse_jinja_variable(variable)
                if parsed_variable:
                    template_variables.add(parsed_variable)

    return template_variables


def parse_jinja_variable(variable, suffix=''):
    if type(variable) is nodes.Name:
        variable_key = join_keys(variable.name, suffix)
        return variable_key
    elif type(variable) is nodes.Getattr:
        return parse_jinja_variable(variable.node, join_keys(variable.attr, suffix))


def join_keys(parent_key, child_key):
    key = child_key if child_key else parent_key
    if parent_key and child_key:
        key = parent_key + '.' + key
    return key


if __name__ == "__main__":
    variable_keys = get_variables({replace_with_your_template directory}, {replace_with_your_template_file})
    print(*variable_keys, sep='\n')


For me jinja2.meta.find_undeclared_variables(parsed_content) is not a good fit because it does not provide nested variables.

jinja2schema tool was kinda ok for simple scenarios but with all the loops and other jinja2 dark powers it was failing with errors.

I have played around with jinja2 data structures and was able to get all variables including nested ones. For my use case this was enough. Maybe this will also help for somebody else :)

Here is the code:

from jinja2 import Environment, FileSystemLoader, nodes


def get_variables(path, filename):
    template_variables = set()
    env = Environment(loader=FileSystemLoader(searchpath=path))
    template_source = env.loader.get_source(env, filename)[0]
    parsed_content = env.parse(template_source)
    if parsed_content.body and hasattr(parsed_content.body[0], 'nodes'):
        for variable in parsed_content.body[0].nodes:
            if type(variable) is nodes.Name or type(variable) is nodes.Getattr:
                parsed_variable = parse_jinja_variable(variable)
                if parsed_variable:
                    template_variables.add(parsed_variable)

    return template_variables


def parse_jinja_variable(variable, suffix=''):
    if type(variable) is nodes.Name:
        variable_key = join_keys(variable.name, suffix)
        return variable_key
    elif type(variable) is nodes.Getattr:
        return parse_jinja_variable(variable.node, join_keys(variable.attr, suffix))


def join_keys(parent_key, child_key):
    key = child_key if child_key else parent_key
    if parent_key and child_key:
        key = parent_key + '.' + key
    return key


if __name__ == "__main__":
    variable_keys = get_variables({replace_with_your_template directory}, {replace_with_your_template_file})
    print(*variable_keys, sep='\n')


一曲爱恨情仇 2024-12-25 01:14:17

为什么不使用正则表达式?

如果发现使用正则表达式更容易:

import re
with open('templates/templatename.html') as f:
    variables = re.findall("\{\{\s(.*?)\s\}\}", f.read())

Why not regex?

If find it a lot easier to use regex:

import re
with open('templates/templatename.html') as f:
    variables = re.findall("\{\{\s(.*?)\s\}\}", f.read())
2024-12-25 01:14:17

基于 @Kracekumar 的答案,但对于最简单的用例,即从作为字符串参数传递的模板中提取令牌,没有加载语义或过滤器覆盖:

env = jinja2.Environment()
parsed_content = env.parse(template_source)
tokens = jinja2.meta.find_undeclared_variables(parsed_content)

tokens 将是一个集合。

Based on @Kracekumar's answer, but for the simplest use-case of just extracting tokens from a template passed as a string argument with no loading semantics or filter overrides:

env = jinja2.Environment()
parsed_content = env.parse(template_source)
tokens = jinja2.meta.find_undeclared_variables(parsed_content)

tokens will be a set.

吻泪 2024-12-25 01:14:17

尽管最上面的答案附有支持extends的注释,但实际上并非如此。 env.loader.get_source 只能解析当前模板。

这是 Violet Shreve 在最佳答案中的评论提供的建议的实现。


import re, os
from jinja2 import Environment, meta, FileSystemLoader

template_path = 'the path to your templates'
def parse_template_variables(template_name):
    res = set()
    env = Environment(loader=FileSystemLoader(template_path))
    # considering the including and the extending
    stack = [template_name]
    while len(stack) > 0:
        ref_template = stack.pop()
        source, filename, uptodate = env.loader.get_source(env, ref_template)
        parsed_content = env.parse(source)
        res = res.union(meta.find_undeclared_variables(parsed_content))
        # Recursively check if there are other dependencies
        for ref_temp_name in meta.find_referenced_templates(parsed_content):
        stack.append(ref_temp_name)
    return res

Though the top answer attaches a note supporting extends, actually, it's not. The env.loader.get_source can only parse the current template.

Here's an implementation for the advice provided by the Violet Shreve's comment in the top answer.


import re, os
from jinja2 import Environment, meta, FileSystemLoader

template_path = 'the path to your templates'
def parse_template_variables(template_name):
    res = set()
    env = Environment(loader=FileSystemLoader(template_path))
    # considering the including and the extending
    stack = [template_name]
    while len(stack) > 0:
        ref_template = stack.pop()
        source, filename, uptodate = env.loader.get_source(env, ref_template)
        parsed_content = env.parse(source)
        res = res.union(meta.find_undeclared_variables(parsed_content))
        # Recursively check if there are other dependencies
        for ref_temp_name in meta.find_referenced_templates(parsed_content):
        stack.append(ref_temp_name)
    return res
凉风有信 2024-12-25 01:14:17

下面是可用于获取变量属性的示例代码
就像 {{ Something.nested }} 一样,不使用任何第三方库。

代码

from jinja2 import Environment, Template, meta
from jinja2.nodes import Getattr, Getitem

def get_placeholders_from_template(content: str):
    environment: Environment = Environment()
    parsed_content: Template = environment.parse(content)
    nodes = parsed_content.body[0].nodes
    parent_nodes = meta.find_undeclared_variables(parsed_content)
    attr_nodes = [node for node in nodes if isinstance(node, Getattr)]
    item_nodes = [node for node in nodes if isinstance(node, Getitem)]
    placeholders = []
    for node in attr_nodes:
        placeholders.append(f"{node.node.name}.{node.attr}")

    for node in item_nodes:
        placeholders.append(f"{node.node.name}({node.arg.value})")

    print("variables: ", parent_nodes)
    print("Node Placeholders: ", placeholders)

输入

content = ("Hello {{ customer.first_name }} {{ customer.last_name }}, "        
           "Appraisal Progress: {{ appraisal.progress }} JobID: {{ Job['1234'] }}")

get_placeholders_from_template()

输出

variables:  ['customer', 'appraisal', 'Job']
Node Placeholders:  ['customer.first_name', 'customer.last_name', 'appraisal.progress', 'Job(1234)']

注意:

可以修改此代码以执行递归操作以获取更深层次的嵌套值,例如 {{ some.more.some_more.once_more }}

Below is the sample code that can be used get properties of the vars
like {{ something.nested }}, without using any third party library.

Code

from jinja2 import Environment, Template, meta
from jinja2.nodes import Getattr, Getitem

def get_placeholders_from_template(content: str):
    environment: Environment = Environment()
    parsed_content: Template = environment.parse(content)
    nodes = parsed_content.body[0].nodes
    parent_nodes = meta.find_undeclared_variables(parsed_content)
    attr_nodes = [node for node in nodes if isinstance(node, Getattr)]
    item_nodes = [node for node in nodes if isinstance(node, Getitem)]
    placeholders = []
    for node in attr_nodes:
        placeholders.append(f"{node.node.name}.{node.attr}")

    for node in item_nodes:
        placeholders.append(f"{node.node.name}({node.arg.value})")

    print("variables: ", parent_nodes)
    print("Node Placeholders: ", placeholders)

Input

content = ("Hello {{ customer.first_name }} {{ customer.last_name }}, "        
           "Appraisal Progress: {{ appraisal.progress }} JobID: {{ Job['1234'] }}")

get_placeholders_from_template()

Output

variables:  ['customer', 'appraisal', 'Job']
Node Placeholders:  ['customer.first_name', 'customer.last_name', 'appraisal.progress', 'Job(1234)']

Note:

This code can be modified to perform a recursive operation to fetch deeper nested values like {{ something.more.some_more.once_more }}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文