我正在使用 jinja 2 输出 yaml 文件,但似乎无法摆脱尾随换行符和 for 循环的末尾。例如下面的
- request:
path: {{ path }}
headers:
origin: 'somedomain.com'
user-agent: 'agent'
referer: 'some.domain.com'
authority: 'somedomain.com'
querystring:
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{% endfor %}
response:
content:
file: {{ content }}
给我输出:
- request:
path: /some/path
headers:
origin: 'somedomain.com'
user-agent: 'agent'
referer: 'somedomain.com'
authority: 'somedomain.com'
querystring:
postcode: 'xxxxxx'
houseNo: '55'
response:
content:
file: address.json
houseNo 之后有一个额外的不需要的空行。我该如何摆脱这条线?
更改循环以从输出的顶部和底部去除空格(注意额外的-
at the for
循环关闭):
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{%- endfor %}
在我的测试中(使用https://github.com/abourguignon/jinja2-live-parser https://github.com/abourguignon/jinja2-live-parser), the -
必须在第一个之后{%
,而不是在最后实现您所要求的之前。
Docs: https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)