Phoenix.View.render doesn't trim newline characters at end of files - should it?

Because of how POSIX defines a line some people consider it ‘good practice’ to have a newline at the end of a file. Many editors are set up to do this automatically.

However the Phoenix render function (using EEX) does not ignore this newline character which can result in unexpected/inconvenient behaviour such as the following:

# _text.html.eex (with a newline at end to make it a valid POSIX line)
@text

iex(1)> Phoenix.View.render(PageView, "_text.html", text: "text")
{:safe, ["" | "@text\n"]}

Note the newline at the end. This causes issues when rendering multiple templates next to each other and you don’t want newlines/spaces inserted.

Would you consider this a bug? If not, is there a workaround (other than removing the newline at the end of the file)?

Thanks.

How would Phoenix differentiate between an intentional EOL character and an unintentional one? Personally if I want a newline I’ll add a new line, thanks. If the editor starts doing that automatically I’m either swiftly disabling that option, or dumping it.

1 Like

That’s quite simple - only the last EOL character would be ignored. I believe many mainstream editors automatically add newlines by default, which editor do you use?

Vim. Usually I set

set noendofline
set nofixendofline
1 Like

Yeah I wouldn’t want it to magically strip newlines. They are very important in some formats.

1 Like