jinja2 如何删除尾随换行符
jinja2 how to remove trailing newline
我正在使用 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. 后面多了一个不需要的空行。我如何去掉这条线?
我认为您可以使用 whitespace control 功能摆脱它。因此,我会将 endfor
块修改为 {% endfor -%}
看看能不能做到!
更改循环以去除输出顶部和底部的空白(注意 for
循环关闭处的额外 -
):
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{%- endfor %}
在我的测试中(使用 https://github.com/abourguignon/jinja2-live-parser),-
必须在第一个 {%
之后,而不是在最后一个之前才能达到您的要求。
文档:https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control
我找到了解决这个问题的方法:
- 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 }}
>
, |
: "clip": 保留换行,去掉结尾的空行。
>-
, |=
: "strip": 去掉换行符,去掉结尾的空行。
>+
, |+
: "keep": 保持换行,保持尾随空行。
Thx Steve Bennett 的 post:
In YAML, how do I break a string over multiple lines?
您可以抑制以下行的呈现:
<% for ... %>
<% endfor %>
<% if ... %>
<% endif %>
通过在您的 jinja2 环境中设置 trim_blocks=True 和 lstrip_blocks=True。请参阅下面的示例,信息来自 their docs
context = {'querystring': querystring, 'path': path, 'content': content}
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader('templates/'),
trim_blocks=True,
lstrip_blocks=True
)
print(jinja_env.get_template('my_template.yaml').render(context))
对于那些使用 Flask 的人来说,这些行对我有用:
app = Flask(__name__)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
接受的答案只是解决方案的一半,因为它删除了 所有 换行符。
您可以通过首先删除所有换行符(在 for 循环中使用 -%}
和 {%-
处的减号)来避免尾随换行符,然后在正确的位置插入所需的换行符 (使用 loop.last
条件)。
以下模板将字典 d 呈现为 JSON 文本:
{
{% for key, value in d.items() -%}
"{{ key }}": "{{ value }}"{{ ",
" if not loop.last }}
{%- endfor %}
}
对于 d = {'a':'1', 'b':'2'}
,模板呈现为
{
"a": "1",
"b": "2"
}
我正在使用 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. 后面多了一个不需要的空行。我如何去掉这条线?
我认为您可以使用 whitespace control 功能摆脱它。因此,我会将 endfor
块修改为 {% endfor -%}
看看能不能做到!
更改循环以去除输出顶部和底部的空白(注意 for
循环关闭处的额外 -
):
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{%- endfor %}
在我的测试中(使用 https://github.com/abourguignon/jinja2-live-parser),-
必须在第一个 {%
之后,而不是在最后一个之前才能达到您的要求。
文档:https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control
我找到了解决这个问题的方法:
- 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 }}
>
,|
: "clip": 保留换行,去掉结尾的空行。>-
,|=
: "strip": 去掉换行符,去掉结尾的空行。>+
,|+
: "keep": 保持换行,保持尾随空行。
Thx Steve Bennett 的 post: In YAML, how do I break a string over multiple lines?
您可以抑制以下行的呈现:
<% for ... %>
<% endfor %>
<% if ... %>
<% endif %>
通过在您的 jinja2 环境中设置 trim_blocks=True 和 lstrip_blocks=True。请参阅下面的示例,信息来自 their docs
context = {'querystring': querystring, 'path': path, 'content': content}
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader('templates/'),
trim_blocks=True,
lstrip_blocks=True
)
print(jinja_env.get_template('my_template.yaml').render(context))
对于那些使用 Flask 的人来说,这些行对我有用:
app = Flask(__name__)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
接受的答案只是解决方案的一半,因为它删除了 所有 换行符。
您可以通过首先删除所有换行符(在 for 循环中使用 -%}
和 {%-
处的减号)来避免尾随换行符,然后在正确的位置插入所需的换行符 (使用 loop.last
条件)。
以下模板将字典 d 呈现为 JSON 文本:
{
{% for key, value in d.items() -%}
"{{ key }}": "{{ value }}"{{ ",
" if not loop.last }}
{%- endfor %}
}
对于 d = {'a':'1', 'b':'2'}
,模板呈现为
{
"a": "1",
"b": "2"
}