Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/HAML: Line breaks in text mail

I'm just trying to format a haml-generated (text) mailer template, and I'm having a little difficulty getting it to read multiple line breaks. Eg: I'd have thought

Dear
= @user.name,

Your username is
= @user.username




Your status is
= @user.status

I had assumed that the multiple line breaks would be read, but the "Your status is" line comes out on the line directly beneath the username. (Yes, that many line breaks is an exaggeration of how many I want, but still)

So, the question is: Line breaks in haml text messages....erm, how?

like image 715
PlankTon Avatar asked May 16 '11 07:05

PlankTon


4 Answers

You can also use \ or ==:

Dear
= @user.name,
\
Your username is
= @user.username
\
\
\
\
Your status is
= @user.status

This has the added advantage of allowing you to use interpolation, whereas using the :plain filter won't.

like image 73
Ivan Avatar answered Nov 18 '22 11:11

Ivan


I'd suggest that using haml for plain text templates doesn't add anything and in most cases makes them more complex then plain old erb templates. It's main purpose, after all, is making markup simple and omitting the need for closing tags - this doesn't apply to plain text.

If you're sending multi-mime emails, there's nothing preventing you from using html.haml for the HTML templates and text.erb for the plain text ones, which will preserve your multiple line breaks:

Dear <%= @user.name %>,

Your username is <%= @user.username %>




Your status is <%= @user.status %>
like image 21
Thilo Avatar answered Nov 18 '22 12:11

Thilo


Try haml's :plain helper.

Dear
= @user.name,

Your username is
= @user.username
:plain



  Your status is
  = @user.status

*edit - you need to indent your haml text following the :plain filter as you would normally within haml.

like image 10
Max Williams Avatar answered Nov 18 '22 11:11

Max Williams


Consider another HAML approach that is more legible.

Dear #{@user.name},

Your username is #{@user.username}
\
\
\
Your status is #{@user.status}

The filename would be something like mailer.text.haml

like image 8
scarver2 Avatar answered Nov 18 '22 12:11

scarver2