2016-11-22 131 views
0

我有以下jinja2模板。当我渲染它时,“endif”语句后面的行没有正确的缩进。我试过传递trim_blocks = True和keep_trailing_newline = False没有太大的成功。Python jinja2注意和空白问题

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" 

实际结果。 部门支付块不按照数据的结构层次

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" 

预期结果。 部门支付对齐与支付版本

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

我想知道我可能会错过什么。

感谢,

回答

0

这怎么我终于解决了这个问题:

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

-%}会吃掉毕竟空白那架(包括新行)。我认为你可能会想在开幕支架({%-)使用-

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" 
+0

谢谢你的提示! '{% - 如果环境不是none:%} 环境: {% - 为键,在environment.items值()%} - {{键}}:{{值}} {% - endfor - %} {% - endif%}' –