Avoid extra newlines after Mako blocks/tags

Viewed 876

I have a Mako template with some blocks in it like this:

<%page />
% for x in ["one", "two", "three"]:
<%
    y = ''.join(reversed(x))
%>
${x} backwards is ${y}
% endfor

When I render this template I get something with a lot of extra blank lines:



one backwards is eno

two backwards is owt

three backwards is eerht

I want the output to look like this:

one backwards is eno
two backwards is owt
three backwards is eerht

Is there a way to do this, other than manually adding a \ after every block and tag?

There's a similar question about this here but it's 9 years old, and the accepted answer is to post-process the Mako output. This is not a great solution for my use case since I want to preserve blank lines in the output except those which came from a block/tag, and it's non-trivial to determine which those are.

1 Answers

Try changing the line endings of the file from CRLF to LF.

At least that worked for me.

My issue was a little different, though. If your extra line comes from the line ending after the %>, I think \ is the only solution.

Related