Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja - Indentation in a recursive loop with dictionary

I'm trying to write a template using ninja. But I can't display the right indentation! I tried a lot of stuffs but I couldn't get the expected result. I have a dictionary like this:

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

I would like to display something like that

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

but all I can get is:

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

Here is my file:

{%- for key, value in videoNodes.iteritems() recursive -%}
    {%+ if value is mapping -%}
        {{ key }}:
        {{ loop(value.iteritems()) }}
    {%- else -%}
       {{ key }}: {{value}}
    {% endif %}
{%- endfor -%}
like image 652
Romanzo Criminale Avatar asked Mar 21 '23 05:03

Romanzo Criminale


2 Answers

You must try using indent function and asign a value depends of your level of indentation:

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

You can see the documentation here

like image 132
Pirata21 Avatar answered Mar 31 '23 22:03

Pirata21


I found it helpful to use multiple indent filters to keep my yaml file indentation clear. It was getting a little confusing otherwise:

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 %}
like image 44
Josiah Avatar answered Mar 31 '23 21:03

Josiah