我怎样才能在 jinja2 python 中换行?
How can I do line break in jinja2 python?
如何在 python 中的 jinja2 中换行?
下面是我的代码
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 %}")
此代码将导致:
由于它横向太长,我想把它们写成几行。
但是,如果我像下面那样做我通常在 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 %}")
结果会是
谁能帮我解决这个问题?
谢谢。
Python 保留空间,因此您也会在结果中看到它们。
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);
你不应该像 . In your case take advantage of parentheses and implicit string concatenation 这样使用字符串连接。
t1 = Template("{% for i in range(0, a1) %}|{{ mylist1[i] }}\n"
" {% for j in range(0, (20 - (mylist1[i]|length))) %}\n"
" {{ space }}\n"
" {% endfor %}|{{ dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]] }}\n"
" {% for j in range(0, (20 - (dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]]|length))) %}\n"
" {{ space }}\n"
" {% endfor %}|\n{{ string }}\n" # Notice "\n" to keep it for Jinja.
"{% endfor %}")
如何在 python 中的 jinja2 中换行?
下面是我的代码
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 %}")
此代码将导致:
但是,如果我像下面那样做我通常在 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 %}")
结果会是
谁能帮我解决这个问题?
谢谢。
Python 保留空间,因此您也会在结果中看到它们。
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);
你不应该像
t1 = Template("{% for i in range(0, a1) %}|{{ mylist1[i] }}\n"
" {% for j in range(0, (20 - (mylist1[i]|length))) %}\n"
" {{ space }}\n"
" {% endfor %}|{{ dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]] }}\n"
" {% for j in range(0, (20 - (dicts[mylist1[i]][dicts[mylist1[i]].keys()[0]]|length))) %}\n"
" {{ space }}\n"
" {% endfor %}|\n{{ string }}\n" # Notice "\n" to keep it for Jinja.
"{% endfor %}")