Skip to content

Conversation

wking
Copy link
Contributor

@wking wking commented Jan 11, 2013

This option defaults to False for compatibility with the existing behaviour.
I've added the option because I expect I won't remember to keep an extra
trailing newline in my templates, and some non-HTML templates need that last
newline.

See also: https://groups.google.com/d/msg/pocoo-libs/6DylMqq1voI/GXTbZJ1Tr-sJ

On 2011-02-28, Armin Ronacher wrote:

I will update the documentation for sure and consider adding a flag
that controls the trailing one.

Here's the flag, all done up with a bow and a unittest ;).

This option defaults to False for compatibility with the existing
behaviour.  I've added the option because I expect I won't remember to
keep an extra trailing newline in my templates, and some non-HTML
templates *need* that last newline.

See also:
https://groups.google.com/d/msg/pocoo-libs/6DylMqq1voI/GXTbZJ1Tr-sJ
@gforcada
Copy link

a 👍 I'm also interested in that.

@wking
Copy link
Contributor Author

wking commented May 18, 2013

Since there's a lot of Jinja work going on at the moment, maybe someone can merge this? :D.

@mitsuhiko mitsuhiko merged commit 7e912c6 into pallets:master May 19, 2013
@dpursehouse
Copy link

Is it intentional/expected that keep_trailing_newline is only effective when passed into the constructor, and not when invoking render()?

For example, I have a template file that has a trailing newline:

>>> from jinja2 import Template
>>> from os.path import abspath, expanduser
>>> data = open(abspath(expanduser("~/jinjatest.txt"))).read()
>>> print "[%s]" % data
[line 1
line 2
]

When it gets rendered, the newline is removed:

>>> template = Template(data)
>>> print "[%s]" % template.render()
[line 1
line 2]

When rendering with keep_trailing_newline=True it is also removed:

>>> print "[%s]" % template.render(keep_trailing_newline=True)
[line 1
line 2]

But if I pass keep_trailing_newline=True when constructing the Template, it works as expected:

>>> template = Template(data, keep_trailing_newline=True)
>>> print "[%s]" % template.render()
[line 1
line 2
]

@wking wking deleted the keep-trailing-newline branch June 6, 2014 17:06
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants