小编ton*_*oni的帖子

如何使用Jinja for循环连接字符串?

我试图迭代地连接一个字符串,用'for'循环构建url params,但我相信我有范围问题.

The output should be: url_param = "&query_param=hello&query_param=world"

array_of_objects = [{'id':'hello'},{'id':'world'}]

{% set url_param = "" %}

{% set array_of_ids = array_of_objects|map(attribute='id')|list%} // correctly returns [1,2]

{% for id in array_of_ids %}
   {% set param = '&query_param='~id %}
   {% set url_param = url_param~param %}                             
{% endfor %}

//url_param is still an empty string
Run Code Online (Sandbox Code Playgroud)

我也尝试过namespace(),但无济于事:

{% set ns = namespace() %}
 {% set ns.output = '' %}
 {% set array_of_ids = array_of_objects|map(attribute='id')|list%} // correctly returns [1,2]
{% for id in …
Run Code Online (Sandbox Code Playgroud)

jinja2 email-templates sendwithus

4
推荐指数
1
解决办法
897
查看次数

标签 统计

email-templates ×1

jinja2 ×1

sendwithus ×1