Jinja2约束和空白问题

Jinja2约束和空白问题

本文介绍了Python Jinja2约束和空白问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有以下jinja2模板.当我渲染它时,"endif"语句之后的行没有适当的缩进.我尝试传递trim_blocks = True和keep_trailing_newline = False并没有太大的成功.

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_newline=False without much success.

   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"

实际结果. 部门付费块不遵循数据结构层次结构

Actual results. departments and paid blocks do not follow the data structure hierarchy

applications:
  appication1:
    version: "1.0"
    dependencies:
      - database: mysql
      - storage: nfs
      departments: <- Indent is not correct
      - accounting: payroll
      paid: "yes" <- Indent is not correct
    obsolete: "no"

预期结果. 部门付费付费版本等保持一致

Expected results. departments and paid align with paid, version and etc.

applications:
  appication1:
    version: "1.0"
    dependencies:
      - database: mysql
      - storage: nfs
    departments:
      - accounting: payroll
    paid: "yes"
    obsolete: "no"

我想知道我还可能缺少什么.

I am wondering what else I could be missing.

谢谢

推荐答案

我终于解决了这个问题:

This how i finally solved it:

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

这篇关于Python Jinja2约束和空白问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:30