如何在jinja2 python中换行? [英] How can I do line break in jinja2 python?

查看:733
本文介绍了如何在jinja2 python中换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中的jinja2中换行?

How can I do line break in jinja2 in python?

下面是我的代码

t1 = Template("{% for i in range(0, a1) %}|{{ mylist1[i] }}{% for j in range(0, (20 - (mylist1[i]|length))) %}{{ space }}{% endfor %}|{{ dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]] }}{% for j in range(0, (20 - (dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]]|length))) %}{{ space }}{% endfor %}|\n{{ string }}{% endfor %}")

此代码将导致: 由于水平方向太长,因此我想将它们写成几行.

This code will result in: Since it is horizontally too long, I want to write them in few lines.

但是,如果我像在python中那样做通常的工作,如下所示:

However, If I do what I usually do in python like below:

t1 = Template("{% for i in range(0, a1) %}|\
               {{ mylist1[i] }}\
               {% for j in range(0, (20 - (mylist1[i]|length))) %}\
                    {{ space }}\
               {% endfor %}|\
               {{ dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]] }}\
               {% for j in range(0, (20 - (dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]]|length))) %}\
                   {{ space }}\
               {% endfor %}|\n\
               {{ string }}\
               {% endfor %}")

结果将是

有人可以帮我解决这个问题吗?

Can anyone help me to solve this?

谢谢.

推荐答案

Python保留空间,因此您也会在结果中看到它们.

Python preserver spaces so you will see them in the results as well.

str = "{% for i in range(0, a1) %}|\"
str += "{{ mylist1[i] }}\"
str += "{% for j in range(0, (20 - (mylist1[i]|length))) %}\"
str += "{{ space }}\"
str += "{% endfor %}|\"
str += "{{ dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]] }}\"
str += "{% for j in range(0, (20 - (dicts[mylist1[i]]"
str += "[dicts[mylist1[i]].keys()[0]]|length))) %}\"
str += "{{ space }}\"
str += "{% endfor %}|\n\"
str += "{{ string }}\"
str += "{% endfor %}")"

# and then use the generates string
t1 = Template(str);

这篇关于如何在jinja2 python中换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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