本文介绍了Jinja中的字符串串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想遍历现有列表,并用逗号分隔字符串.
像这样:my_string = 'stuff, stuff, stuff, stuff'
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'
我已经了解了loop.last
,我只需要知道如何在我的代码下将第三行写到WORK.
I already know about loop.last
, I just need to know how to make the third line in my code below WORK.
{% set my_string = '' %}
{% for stuff in stuffs %}
{% set my_string = my_string + stuff + ', '%}
{% endfor%}
推荐答案
如果stuffs
是字符串列表,则可以使用:
If stuffs
is a list of strings, just this would work:
{{ stuffs|join(", ") }}
Link to join
filter documentation, link to filters in general documentation.
p.s.
更多读者友好的方式{{我的〜','〜字符串}}
More reader friendly way{{ my ~ ', ' ~ string }}
这篇关于Jinja中的字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!