Skip to content Skip to sidebar Skip to footer

Python Jinja2 Indentention And Whitespace Issue

I have the following jinja2 template. When I render it, lines after the 'endif' statement do not have proper indentation. I've tried passing trim_blocks=True and keep_trailing_newl

Solution 1:

This how i finally solved it:

{%- if environment is not none: %}
enviroment:
{%- for key, value in environment.items() %}
  - {{ key }}: {{ value }}
{%- endfor -%}
{%- endif %}

Solution 2:

The -%} will eat up all whitespace after that bracket (including newlines). I think that you'll probably want to use the - on the opening bracket ({%-):

   applications:
      {{ application_name }}:
        version: {{ version }}
        {% if dependencies is not none: -%}
        dependencies:
          {% for key, value in dependencies.items() -%}
            - {{ key }}: {{ value }}
          {% endfor -%}
        {% endif -%}
        {%- if departments is not none: %}
        departments:
          {% for key, value in departments.items() -%}
            - {{ key }}: {{ value }}
          {% endfor -%}
        {%- endif %}
        paid: "yes"
        obsolete: "no"

Post a Comment for "Python Jinja2 Indentention And Whitespace Issue"