Jinja2 模板:输出格式

Jinja2 template: output format

我对 jinja2 模板很陌生,我尝试使用 jinja 模板获取输出数据。

data_list=

[
    {
        "v4-filter": {
            "accept_1": [
                {
                    "destination-prefix-list": "v4-future-prefix-list-1"
                },
                {
                    "source-prefix-list": "v4-future-prefix-list-2"
                },
                                {
                    "source-prefix-list": "v4-future-prefix-list-3"
                },
                {
                    "destination-port": "80"
                },
                {
                    "destination-port": "443"
                },
                {
                    "action": "accept"
                }
            ],
            "accept_2": [
                {
                    "destination-prefix-list": "v4-future-prefix-list4"
                },
                {
                    "source-prefix-list": "v4-future-prefix-list5"
                },
                {
                    "action": "accept"}]}}]

我的神社模板

{%- for d  in data_list -%} 
    {%- for k,v  in d.items() -%} 
        {%- for term,value  in v.items() -%}
          term:: {{ term }} 
          {%- for dict_item in value -%}
            {%- for key, value in dict_item.items() -%}
               {% if  key == "source-prefix-list" %}
                 source-address:: {{value}} 
               {% endif %}
               {% if  key == "destination-prefix-list" %}
                 destination-address:: {{value}} 
               {% endif %}
               {% if  key == "destination-port" %}
                 destination-port:: {{value}} 
               {% endif %}
               {% if  key == "action" %}
                 action:: {{value}} 
               {% endif %}
            {%- endfor -%}
          {%- endfor -%}
        {%- endfor -%}
    {%- endfor -%} 
{%- endfor -%} 

我得到的输出

term:: accept_1
               
                 destination-address:: v4-future-prefix-list-1 
               
               
               
                 source-address:: v4-future-prefix-list-2 
               
               
               
               
                 source-address:: v4-future-prefix-list-3 
               
               
               
               
               
               
                 destination-port:: 80 
               
               
               
               
                 destination-port:: 443 
               
               
               
               
               
                 action:: accept 
               term:: accept_2
               
                 destination-address:: v4-future-prefix-list4 
               
               
               
                 source-address:: v4-future-prefix-list5 
               
               
               
               
               
               
               
                 action:: accept 

我想要的输出

term accept_1 {
    source-address:: v4-future-prefix-list-1
                     v4-future-prefix-list-2
                     v4-future-prefix-list-3
    destination-port:: 80
                       443
    action:: accept
}

term accept_2 {
    source-address:: v4-future-prefix-list-5
    destination-address:: v4-future-prefix-list-4
    action:: accept
}

我知道 jinja 模板看起来不太好,但最后它包含了我需要的所有信息,现在只是想按预期正确格式化。

我尝试使用嵌套的 if 语句来正确获取结果,但没有成功。

欢迎任何提示。 谢谢,巴勃罗。

以下模板将生成请求的输出:

{%- for d  in data_list -%}
{%- for k,v  in d.items() -%}
{%- for term,value  in v.items() -%}
{%- set label = namespace(source=false, dest=false, port=false) %}
term {{ term }} {
{%- for dict_item in value -%}
{%- for key, value in dict_item.items() -%}
    {%- if key == "source-prefix-list" %}
    {% if not label.source %}source-address::{% else %}{{ ' '*16 }}{% endif %} {{value}}
    {%- set label.source = true %}
    {%- endif %}
    {%- if key == "destination-prefix-list" %}
    {% if not label.dest %}destination-address::{% else %}{{ ' '*21 }}{% endif %} {{value}}
    {%- set label.dest = true %}
    {%- endif %}
    {%- if key == "destination-port" %}
    {% if not label.port %}destination-port::{% else %}{{ ' '*18 }}{% endif %} {{value}}
    {%- set label.port = true %}
    {%- endif %}
    {%- if key == "action" %}
    action:: {{value}}
    {%- endif %}
{%- endfor %}
{%- endfor %}
}
{% endfor %}
{%- endfor %}
{%- endfor %}

输出:

term accept_1 {
    destination-address:: v4-future-prefix-list-1
    source-address:: v4-future-prefix-list-2
                     v4-future-prefix-list-3
    destination-port:: 80
                       443
    action:: accept
}

term accept_2 {
    destination-address:: v4-future-prefix-list4
    source-address:: v4-future-prefix-list5
    action:: accept
}

这里我们使用一个Jinja2 namespace object,我们可以用它来跟踪标签在当前周期中是否已经打印过。如果打印标签,我们翻转 label namespace 变量中的布尔跟踪变量,然后打印足够的 space 以正确对齐下一个打印值。