Removing whitespace in EEX templates

Hi,
What are people doing to avoid unintentionally introducing whitespace between tags in EEx? For example, I can do:

<span>A sentence starts,</span>
<%= "and then we introduce some dynamic content" %>
<span>.  But then we're left with unintentional whitespace before the period.</span>
  • Can put everything all on one line, but that can get pretty messy with long sequences.
  • Can generate the markup in a view helper, but that’s kinda cumbersome

EEx has an option to trim around quotation tags, but I’m not sure how to set it globally in a phoenix project, not to mention the potential unforseen consequences of doing that.

Thanks for any insight

Edit: code formatting

I wouldn’t care about it unless there is a significant performance benefit to removing whitespace.

Well the whitespace there is entirely expected as you specifically put newlines around it. If you don’t want whitespace then just don’t put whitespace:

<span>A sentence starts,</span><%= "and then we introduce some dynamic content" %><span>.  But then we're left with unintentional whitespace before the period.</span>

Which of course you can still put on multiple lines if you want (inside the escape):

<span>A sentence starts,</span><%=
  "and then we introduce some dynamic content"
%><span>.  But then we're left with unintentional whitespace before the period.</span>

:slight_smile:

Yeah, ‘unintended’ was a poor word choice…should’ve said ‘undesirable.’

@OvermindDL1 at least in the case I’m working on, EEx chokes on mid-interpolation newlines . But thanks anyway!

Hmm? Do you have an example?

NVM. Tried it again and it works. I guess I put the line breaks in the wrong places the first time. Thanks!

1 Like