【发布时间】:2020-09-30 18:03:10
【问题描述】:
我有一个包含 for 循环的 Jinja2 模板。我希望 for 循环中的每个项目都用换行符分隔(包括第一项之前的换行符)。但是,如果没有在循环后出现额外的换行符,我就无法实现这一点。
{% set proxy_interfaces = {"Vlan2000": ["fc02:1000::/64"], "Vlan3000": ["fc03:1000::/64"]} %}
{% for intf, prefix_list in proxy_interfaces.items() %}
{% if prefix_list %}
proxy {{ name }} {
{% for prefix in prefix_list %}
prefix {{ prefix }} {
static
}
{% endfor %}
}
{% endif %}
{% endfor %}
实际输出是
<newline>
proxy {
prefix fc03:1000::/64 {
static
}
}
proxy {
prefix fc02:1000::/64 {
static
}
}
<newline>
我想要的输出是:
<newline>
proxy {
prefix fc03:1000::/64 {
static
}
}
proxy {
prefix fc02:1000::/64 {
static
}
}
我尝试将空白控制字符 - 和 + 添加到循环开始和结束以及 if 标记的不同位置,但无法实现所需的输出。不幸的是,我的设置无法编辑 Jinja 环境以更改 trim_blocks 和其他参数。
【问题讨论】:
标签: jinja2