Phoenix: Unwanted whitespace trimming in EEx templates

Created on 26 Feb 2020  路  5Comments  路  Source: phoenixframework/phoenix

This is a known issue (#3482) but it was closed some time ago.
We are experiencing similar issues which are blocking us from updating to Elixir v1.9.0 and up.

According to the Elixir v1.9.0 release notes:

  1. Bug fixes
    [EEx] Consistently trim newlines when you have a single EEx expression per line on multiple lines

What bug does this actually fix?

Edit: https://github.com/elixir-lang/elixir/issues/8792 fixed via https://github.com/phoenixframework/phoenix/commit/04295d8eb19ff7c65bd9f2b8f836040bd925c897

For now I would recommend to either go with double spaces or an explicit  .

I would expect everything outside EEx expressions to output exactly the way I write it, only replacing the expressions itself. Writing   or additional newlines feels like a hack rather than a fix. Extra load and error prone because this is not how you would write HTML.

Does this make sense or am I missing a bigger picture here? Please help me understand 馃槅

If this continues to be an issue, we can make trim optional.

That would be awesome!! Would be happy to give this a go.

Example

<%= "Hello" %> <%= "World" %>

"Hello World" (expected)

<%= "Hello" %>
<%= "World" %>

"HelloWorld" (unexpected)

<%= "Hello" %>

<%= "World" %>

"Hello World" (expected)

Most helpful comment

Fixed in Elixir master. It was bigger than I thought, so I won't backport it to v1.10. If you want to make it configurable in Phoenix, a PR is also welcome. Thank you!

All 5 comments

The second example is an EEx bug. The trimming should only trim space after newlines, leaving at least one new line. It will be fixed on Elixir master and v1.10.

Fixed in Elixir master. It was bigger than I thought, so I won't backport it to v1.10. If you want to make it configurable in Phoenix, a PR is also welcome. Thank you!

I have added the option to master and v1.4 too.

I have added the option to master and v1.4 too.

Awesome thank you so much 馃帀
Wanted to give this a go tomorrow but you're too fast 鈿★笍

I was thinking, in Ruby ERB you can enable trim mode per line by adding a - when closing the expression:

<% # avoids line break after expression -%>

Would this be possible and a helpful addition in EEx?

Was this page helpful?
0 / 5 - 0 ratings