Felix
Felix

Reputation: 694

How to control line breaks in plain text email views (.text.erb)

Unlike most plain text email related questions, my problem is that there are too many line breaks in the plain text emails Rails is sending out.

For simplicity while starting up, I ditched HTML emails altogether and just use plain text emails (using .text.erb views). My problems occur where I have conditional lines in the view, as the new line of code in my view file carries over to the email.

For example:

    Line 1
    <%= "Line 2" if false %>
    Line 3

will render as:

    Line 1

    Line 3

and not the intended output:

    Line 1
    Line 3

My current hack is to use the following:

    Line 1
    <%= "Line 2\n" if false %>Line 3

This can get really messy when there are multiple conditionals in a row.

Surely there must be a better way!

Upvotes: 10

Views: 2127

Answers (2)

Richard Jones
Richard Jones

Reputation: 4810

This is to answer Felix' question on Andy Waite's answer (I don't think multi-line code is possible in comments and this question is about multi-line code).

I think <%= "foo\n" if something -%> would work, but this seems cleaner to me:

Line 1
<% if something -%>
foo
<% end -%>
Line 3

Upvotes: 4

Andy Waite
Andy Waite

Reputation: 11076

If you end the ERB tags with -%> that should avoid the extraneous whitespace:

<%= "foo" -%>

Upvotes: 4

Related Questions