开发者

Rails/HAML: Line breaks in text mail

开发者 https://www.devze.com 2023-03-06 16:00 出处:网络
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

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 a开发者_如何转开发n exaggeration of how many I want, but still)

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


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.


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 %>


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.


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


Use %br tag for break line. Also you can use %p and %div and add a bit of css :)

0

精彩评论

暂无评论...
验证码 换一张
取 消