Python jinja2 如何删除尾随换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36870953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:28:15  来源:igfitidea点击:

jinja2 how to remove trailing newline

pythonyamljinja2newline

提问by Yunti

I'm using jinja 2 to output a yaml file but can't seem to get rid of a trailing newline and the end of a for loop. Eg the below

我正在使用 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 }}

gives me the output:

给我输出:

- 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

With an additional unwanted blank line after houseNo. How do I get rid of this line?

在 houseNo. 后面多了一个不需要的空行。我如何摆脱这条线?

回答by tknickman

Change your loop to strip white spaces from the top AND bottom of the output (notice extra "-" at the for loop close):

更改循环以从输出的顶部和底部去除空格(注意 for 循环关闭处的额外“-”):

 {% for key, value in querystring.items() -%}
      {{ key }}: '{{ value }}'
 {%- endfor %}

In my tests (using https://github.com/abourguignon/jinja2-live-parser), the "-" must come after the first {%, not before the last to achieve what you're asking for.

在我的测试中(使用https://github.com/abourguignon/jinja2-live-parser),“-”必须在第一个之后{%,而不是在最后一个之前,以实现您的要求。

Docs: http://jinja.pocoo.org/docs/dev/templates/#whitespace-control

文档:http: //jinja.pocoo.org/docs/dev/templates/#whitespace-control

回答by Scratch'N'Purr

I think you can get rid of it using the whitespace controlfeature. Thus I would modify the endforblock to {% endfor -%}

我认为您可以使用空白控制功能摆脱它。因此,我会将endfor块修改为{% endfor -%}

See if that does it!

看看行不行!

回答by MarredCheese

For those using Flask who arrive here, these lines did the trick for me:

对于那些使用 Flask 的人来说,这些行对我有用:

app = Flask(__name__)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True

回答by shuaiming

I found a way to solve this problem:

我找到了解决这个问题的方法:

- 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": keep the line feed, remove the trailing blank lines.
  • >-, |=: "strip": remove the line feed, remove the trailing blank lines.
  • >+, |+: "keep": keep the line feed, keep trailing blank lines.
  • >, |: "clip": 保持换行,去掉尾随的空行。
  • >-, |=: "strip": 去除换行符,去除尾随空行。
  • >+, |+: "keep": 保持换行,保持尾随空行。

Thx Steve Bennett's post: In YAML, how do I break a string over multiple lines?

Thx Steve Bennett的帖子: 在 YAML 中,如何将一个字符串拆分成多行?

回答by spacether

You can suppress rendering of the below lines:

您可以禁止渲染以下几行:

<% for ... %>
<% endfor %>
<% if ... %>
<% endif %>

by setting trim_blocks=True and lstrip_blocks=True in your jinja2 environment. See the example below, info from their docs

通过在 jinja2 环境中设置 trim_blocks=True 和 lstrip_blocks=True 。请参阅下面的示例,来自他们文档的信息

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))