Jinja中的字符串串联 [英] String concatenation in Jinja

查看:100
本文介绍了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(", ") }}

链接join过滤器文档,链接到常规文档中的过滤器.

Link to join filter documentation, link to filters in general documentation.

p.s.

更多读者友好的方式 {{我的〜','〜字符串}}

More reader friendly way {{ my ~ ', ' ~ string }}

这篇关于Jinja中的字符串串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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