我有这个带有嵌套字典的代码,我需要在没有任何运气的情况下放入 jinja2 模板中......
from jinja2 import Template
vlans = {"1": {"description": "default", "name": "default", "IP": {"IP1": "10.0.0.1", "IP2": "20.0.0.2"}}}
vlan_template = Template('''
{% for key,value in vlans.items() %}
#
vlan {{ key }}
description {{ value.description }}
name {{ value.name }}
{% if value == "IP"%}
{% for subkey,subvalue in value["IP"].items() %}
ip address {{ subvalue.IP1 }}
ip address {{ subvalue.IP2 }} secondary
{% endfor %}
{% endif %}
#
{% endfor %}
''')
print(vlan_template.render(vlans = vlans))
我正在尝试获得以下输出
#
vlan 1
description default
name default
ip address 10.0.0.1
ip address 20.0.0.2 secondary
#
谢谢,马吕斯。
子衿沉夜
相关分类