How can I do line break in jinja2 python?
up vote
0
down vote
favorite
How can I do line break in jinja2 in python?
Below is my code
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.
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 %}")
The result will be
Can anyone help me to solve this?
Thank you.
python jinja2
add a comment |
up vote
0
down vote
favorite
How can I do line break in jinja2 in python?
Below is my code
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.
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 %}")
The result will be
Can anyone help me to solve this?
Thank you.
python jinja2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I do line break in jinja2 in python?
Below is my code
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.
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 %}")
The result will be
Can anyone help me to solve this?
Thank you.
python jinja2
How can I do line break in jinja2 in python?
Below is my code
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.
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 %}")
The result will be
Can anyone help me to solve this?
Thank you.
python jinja2
python jinja2
asked Apr 26 '16 at 13:18
beneditatan
5916
5916
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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);
add a comment |
up vote
0
down vote
You shouldn't use string concatenation like in this answer. 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 %}")
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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);
add a comment |
up vote
2
down vote
accepted
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);
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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);
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);
answered Apr 26 '16 at 13:29


CodeWizard
49k126688
49k126688
add a comment |
add a comment |
up vote
0
down vote
You shouldn't use string concatenation like in this answer. 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 %}")
add a comment |
up vote
0
down vote
You shouldn't use string concatenation like in this answer. 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 %}")
add a comment |
up vote
0
down vote
up vote
0
down vote
You shouldn't use string concatenation like in this answer. 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 %}")
You shouldn't use string concatenation like in this answer. 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 %}")
answered Nov 9 at 11:36
JCode
166110
166110
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36866128%2fhow-can-i-do-line-break-in-jinja2-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown