Jinja docx模板,避免为嵌套新行 [英] Jinja docx template, avoiding new line in nested for

查看:52
本文介绍了Jinja docx模板,避免为嵌套新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在jinja的docx模板中是否有办法避免for内的换行符.代码如下:

I was wondering if there is a way to avoid newline characters inside a for in a docx template with jinja. The code is as follows:

{% for customer in customers %}
{% for account in customer.accounts %}
{{ account.number }}
{% endfor %}{% endfor %}. 

但是结果是帐号用行分隔:

But the result is account numbers separated by row:

234090

29292

29292

我正在LibreOffice上进行测试.

I'm testing on LibreOffice.

有人可以帮我吗?

推荐答案

了解额外的换行符(以及行)来自何处

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

understanding where the extra linebreaks (and thus lines) come from

Whitespace in a Jinja template isn't being ignored. So what Jinja sees is

{% for customer in customers %}¶
{% for account in customer.accounts %}¶
{{ account.number }}¶
{% endfor %}{% endfor %}.·¶

它实际上并不太在乎行,所以使它

And it actually doesn't care about lines too much, so make that

{% for customer in customers %}¶{% for account in customer.accounts %}¶{{ account.number }}¶{% endfor %}{% endfor %}.·¶

这就是循环

{% for customer in customers %}…{% endfor %}.·

随身

¶{% for account in customer.accounts %}¶{{ account.number }}¶{% endfor %}

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

Note the at the beginning. The rest of the outer loop's body is another loop

{% for account in customer.accounts %}…{% endfor %}

随身

¶{{ account.number }}¶

请注意的开头和结尾.

Note the s at the beginning and end.

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

So you'll get a line break before the group accounts of each separate customer, and another line break before and after each account number. You probably don't want to get rid of all of them, because that would glue all the numbers together on a single line without any separation:

2340902929229292

缓解

您可以只避免换行,除了所需的换行:

mitigation

You can just avoid the line breaks except for the ones you want:

{% for customer in customers %}{% for account in customer.accounts %}{{ account.number }}¶
{% endfor %}{% endfor %}.·¶

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

That makes the template hard to read, though. You can let Jinja2 ignore whitespace between template tags. To do that, add a - at the end of the tag preceding the whitespace in question or at the beginning of the tag following that whitespace (or both):

{% for customer in customers -%}
{% for account in customer.accounts -%}
{{ account.number }}
{% endfor -%}
{% endfor %}. 

{% for customer in customers %}
{%- for account in customer.accounts %}
{{- account.number }}
{%- endfor %}
{% endfor %}. 

{% for customer in customers -%}
{%- for account in customer.accounts -%}
{{- account.number }}
{% endfor -%}
{%- endfor %}. 

(请参阅 Jinja2文档)

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

This even allows you to use indentation without having that additional whitespace end up in the result:

{% for customer in customers -%}
  {% for account in customer.accounts -%}
    {{ account.number }}{{ '\n' -}}
  {% endfor -%}
{% endfor %}. 

{% for customer in customers %}
  {%- for account in customer.accounts %}
    {{- account.number }}{{ '\n' }}
  {%- endfor %}
{% endfor %}. 

{% for customer in customers -%}
  {%- for account in customer.accounts -%}
    {{- account.number }}{{ '\n' -}}
  {% endfor -%}
{%- endfor %}. 

我已经利用了这样的事实,即模板标签中不仅可以使用变量,而且可以使用文字,因此我可以使用 {{''n'}} 换行.使用这种样式是必要的,因为吃缩进的-也会吞噬模板源中的(文字)换行符.

I've used the fact that not just variables but also literals can be used in template tags, so that I can produce a line break with {{ '\n' }}. This is necessary with this style, as a - to eat the indentation would swallow the (literal literal) line break in your template source, too.

这篇关于Jinja docx模板,避免为嵌套新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆