【问题标题】:Jinja - Indentation in a recursive loop with dictionaryJinja - 带有字典的递归循环中的缩进
【发布时间】:2014-03-13 22:51:36
【问题描述】:

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

videoNode = {'type': "VideoLoader",
            'config': {'type': "url",
                       'source': "blabla",
                       'frameBufferSize': 50,
                      }
            }

我想展示类似的东西

queueVideo1:
    type: VideoLoader
    config:
        source: blabla
        type: url
        frameBufferSize: 50

但我能得到的只有:

queueVideo1:
    type: VideoLoader
config:
   source: blabla
type: url
frameBufferSize: 50

这是我的文件:

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

【问题讨论】:

    标签: python templates jinja2


    【解决方案1】:

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

    {{ key|indent(2, true) }}
    

    你可以看到the documentation here

    【讨论】:

      【解决方案2】:

      我发现使用多个缩进过滤器来保持我的 yaml 文件缩进清晰很有帮助。否则会变得有点混乱:

      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: {{ name }}
        namespace: {{ namespace }}
      data:
      {% filter indent(2) %}
      customResourceDefinitions: |-
        {% filter indent(2) %}
          {%- for crd in crds -%}
          - {{ crd.content|indent(2) }}
          {%- endfor -%}
        {% endfilter %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-24
        相关资源
        最近更新 更多