Jinja docx模板,避免为嵌套新行

Pat*_*ior 2 python docx jinja2

我想知道是否有一种方法可以避免在Jinja的docx模板中的for中使用换行符。代码如下:

{% for customer in customers %}
{% for account in customer.accounts %}
{{ account.number }}
{% endfor %}{% endfor %}. 
Run Code Online (Sandbox Code Playgroud)

但是结果是帐号被行隔开:

234090

29292

29292

我正在LibreOffice上进行测试。

有人可以帮我吗?

das*_*s-g 7

了解额外的换行符(以及换行)从何而来

Jinja模板中的空白不会被忽略。所以Jinja看到的是

{% for customer in customers %}¶
{% for account in customer.accounts %}¶
{{ account.number }}¶
{% endfor %}{% endfor %}.·¶
Run Code Online (Sandbox Code Playgroud)

它实际上并不太在乎行,因此

{% for customer in customers %}¶{% for account in customer.accounts %}¶{{ account.number }}¶{% endfor %}{% endfor %}.·¶
Run Code Online (Sandbox Code Playgroud)

这就是循环

{% for customer in customers %}…{% endfor %}.·
Run Code Online (Sandbox Code Playgroud)

与身体

¶{% for account in customer.accounts %}¶{{ account.number }}¶{% endfor %}
Run Code Online (Sandbox Code Playgroud)

请注意开头的。外循环主体的其余部分是另一个循环

{% for account in customer.accounts %}…{% endfor %}
Run Code Online (Sandbox Code Playgroud)

与身体

¶{{ account.number }}¶
Run Code Online (Sandbox Code Playgroud)

注意开头和结尾的。

因此,您将在每个单独客户的组帐户之前有一个换行符,在每个帐号之前和之后都有一个换行符。您可能不想摆脱所有这些问题,因为那样会将所有数字粘合在一起而没有任何分隔:

{% for customer in customers %}¶
{% for account in customer.accounts %}¶
{{ account.number }}¶
{% endfor %}{% endfor %}.·¶
Run Code Online (Sandbox Code Playgroud)

减轻

您可以避免换行,除了想要的换行:

{% for customer in customers %}{% for account in customer.accounts %}{{ account.number }}¶
{% endfor %}{% endfor %}.·¶
Run Code Online (Sandbox Code Playgroud)

但是,这使模板难以阅读。您可以让Jinja2忽略模板标签之间的空格。为此,请-在有问题的空白之前的标签末尾或该空白之后的标签开始处添加a (或同时在这两者之后):

{% for customer in customers -%}
{% for account in customer.accounts -%}
{{ account.number }}
{% endfor -%}
{% endfor %}. 
Run Code Online (Sandbox Code Playgroud)

要么

{% for customer in customers %}
{%- for account in customer.accounts %}
{{- account.number }}
{%- endfor %}
{% endfor %}. 
Run Code Online (Sandbox Code Playgroud)

要么

{% for customer in customers -%}
{%- for account in customer.accounts -%}
{{- account.number }}
{% endfor -%}
{%- endfor %}. 
Run Code Online (Sandbox Code Playgroud)

(请参阅Jinja2文档)

这甚至允许您使用缩进,而不会在结果中导致额外的空白:

{% for customer in customers -%}
  {% for account in customer.accounts -%}
    {{ account.number }}{{ '\n' -}}
  {% endfor -%}
{% endfor %}. 
Run Code Online (Sandbox Code Playgroud)

要么

{% for customer in customers %}
  {%- for account in customer.accounts %}
    {{- account.number }}{{ '\n' }}
  {%- endfor %}
{% endfor %}. 
Run Code Online (Sandbox Code Playgroud)

要么

{% for customer in customers -%}
  {%- for account in customer.accounts -%}
    {{- account.number }}{{ '\n' -}}
  {% endfor -%}
{%- endfor %}. 
Run Code Online (Sandbox Code Playgroud)

我已经使用了这样一个事实,即不仅可以在模板标签中使用变量,而且可以使用文字,以便可以使用创建换行符{{ '\n' }}。这种风格是必需的,因为-吃缩进也会吞下模板源中的(文字)换行符。


spa*_*her 6

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

<% for ... %>
<% endfor %>
<% if ... %>
<% endif %>
Run Code Online (Sandbox Code Playgroud)

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

context = {'querystring': querystring, 'path': path, 'content': content}    
loader = jinja2.FileSystemLoader('templates/')
jinja_env = jinja2.Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
print(jinja_env.get_template('my_template.yaml').render(context))
Run Code Online (Sandbox Code Playgroud)