jinja2 how to remove trailing newline
Solution 1:
Change your loop to strip whitespace from the top AND bottom of the output (notice extra -
at the for
loop close):
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{%- endfor %}
In my tests (using https://github.com/abourguignon/jinja2-live-parser), the -
must come after the first {%
, not before the last to achieve what you're asking for.
Docs: https://jinja.palletsprojects.com/en/latest/templates/#whitespace-control
Solution 2:
I think you can get rid of it using the whitespace control feature. Thus I would modify the endfor
block to {% endfor -%}
See if that does it!
Solution 3:
For those using Flask who arrive here, these lines did the trick for me:
app = Flask(__name__)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
Solution 4:
I found a way to solve this problem:
- request:
path: {{ path }}
headers:
origin: 'somedomain.com'
user-agent: 'agent'
referer: 'some.domain.com'
authority: 'somedomain.com'
querystring: >-
{% for key, value in querystring.items() -%}
{{ key }}: '{{ value }}'
{% endfor %}
response:
content:
file: {{ content }}
-
>
,|
: "clip": keep the line feed, remove the trailing blank lines. -
>-
,|=
: "strip": remove the line feed, remove the trailing blank lines. -
>+
,|+
: "keep": keep the line feed, keep trailing blank lines.
Thx Steve Bennett's post: In YAML, how do I break a string over multiple lines?
Solution 5:
The accepted answer is only half of the solution, because it removes all newlines.
You can avoid the trailing newline by first removing all newlines (using the minus signs at -%}
and {%-
in the for loop), and then inserting the desired newlines at the right place (using the loop.last
condition).
The following templates renders a dictionary, d, to as JSON text:
{
{% for key, value in d.items() -%}
"{{ key }}": "{{ value }}"{{ ",
" if not loop.last }}
{%- endfor %}
}
For d = {'a':'1', 'b':'2'}
, the template renders to
{
"a": "1",
"b": "2"
}