string Jinja 中的字符串连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2061439/
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-09-09 00:36:47  来源:igfitidea点击:

String concatenation in Jinja

stringjinja2

提问by KacieHouser

I just want to loop through an existing list and make a comma delimited string out of it.
Something like this: my_string = 'stuff, stuff, stuff, stuff'

我只想遍历现有列表并从中生成一个逗号分隔的字符串。
像这样的东西:my_string = 'stuff, stuff, stuff, stuff'

I already know about loop.last, I just need to know how to make the third line in my code below WORK.

我已经知道了loop.last,我只需要知道如何使代码中的第三行在 WORK 下面。

{% set my_string = '' %}
{% for stuff in stuffs %}
{% set my_string = my_string + stuff + ', '%}
{% endfor%}

回答by mechanical_meat

If stuffsis a list of strings, just this would work:

如果stuffs是一个字符串列表,这将起作用:

{{ stuffs|join(", ") }}

Linkto documentation.

链接到文档。

回答by Dag Wieers

You can use +if you know all the values are strings. Jinja also provides the ~operator, which will ensure all values are converted to string first.

+如果您知道所有值都是字符串,则可以使用。Jinja 还提供了~运算符,它将确保所有值首先转换为字符串。

{% set my_string = my_string ~ stuff ~ ', '%}

回答by KacieHouser

My bad, in trying to simplify it, I went too far, actually stuffsis a record of all kinds of info, I just want the id in it.

我的不好,为了简​​化它,我走得太远了,实际上stuffs是各种信息的记录,我只是想要其中的id。

stuffs = [[123, first, last], [456, first, last]]

I want my_stingto be

我想my_sting成为

my_sting = '123, 456'

My original code should have looked like this:

我的原始代码应该是这样的:

{% set my_string = '' %}
{% for stuff in stuffs %}
{% set my_string = my_string + stuff.id + ', '%}
{% endfor%}

Thinking about it, stuffsis probably a dictionary, but you get the gist.

想想看,stuffs可能是一本字典,但你明白了要点。

Yes I found the joinfilter, and was going to approach it like this:

是的,我找到了join过滤器,并打算像这样处理它:

 {% set my_string = [] %}
 {% for stuff in stuffs %}
 {% do my_string.append(stuff.id) %}
 {% endfor%}
 {%  my_string|join(', ') %}

But the append doesn't work without importing the extensions to do it, and reading that documentation gave me a headache. It doesn't explicitly say where to import it from or even where you would put the import statement, so I figured finding a way to concat would be the lesser of the two evils.

但是如果不导入扩展名,附加就无法工作,阅读该文档让我很头疼。它没有明确说明从哪里导入它,甚至没有说明将 import 语句放在哪里,所以我认为找到一种连接方法是两害相权取其轻。

回答by Kapil

Just another hack can be like this.

只是另一个黑客可能是这样的。

I have Array of strings which I need to concatenate. So I added that array into dictionary and then used it inside for loop which worked.

我有需要连接的字符串数组。所以我将该数组添加到字典中,然后在有效的 for 循环中使用它。

{% set dict1 = {'e':''} %}
{% for i in list1 %}
{% if dict1.update({'e':dict1.e+":"+i+"/"+i}) %} {% endif %}
{% endfor %}
{% set layer_string = dict1['e'] %}

回答by Jakub N

If you can't just use filter join but need to perform some operations on the array's entry:

如果您不能只使用过滤器连接而是需要对数组的条目执行一些操作:

{% for entry in array %}
User {{ entry.attribute1 }} has id {{ entry.attribute2 }}
{% if not loop.last %}, {% endif %}
{% endfor %}