Jinja - 使用字典的递归循环中的缩进

Rom*_*ale 4 python templates jinja2

我正在尝试使用 ninja 编写一个模板。但我无法显示正确的缩进!我尝试了很多东西,但没有得到预期的结果。我有一本这样的字典:

videoNode = {'type': "VideoLoader",
            'config': {'type': "url",
                       'source': "blabla",
                       'frameBufferSize': 50,
                      }
            }
Run Code Online (Sandbox Code Playgroud)

我想展示类似的东西

queueVideo1:
    type: VideoLoader
    config:
        source: blabla
        type: url
        frameBufferSize: 50
Run Code Online (Sandbox Code Playgroud)

但我能得到的是:

queueVideo1:
    type: VideoLoader
config:
   source: blabla
type: url
frameBufferSize: 50
Run Code Online (Sandbox Code Playgroud)

这是我的文件:

{%- for key, value in videoNodes.iteritems() recursive -%}
    {%+ if value is mapping -%}
        {{ key }}:
        {{ loop(value.iteritems()) }}
    {%- else -%}
       {{ key }}: {{value}}
    {% endif %}
{%- endfor -%}
Run Code Online (Sandbox Code Playgroud)

小智 5

您必须尝试使用​​缩进函数并根据缩进级别指定一个值:

{{ key|indent(2, true) }}
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看文档