29

I have an ERB template for sending an email.

Name: <%= @user.name %>
<% if @user.phone.present? %>
Phone: <%= @user.phone %>
<% end %>
Address: <%= @user.address %>

I am trying to remove the blank line between Name and Address when Phone is empty.

Returned result

Name: John Miller 

Address: X124 Dummy Lane, Dummy City, CA

Expected result

Name: John Miller 
Address: X124 Dummy Lane, Dummy City, CA

I have tried to use <%--%> tags(to remove the trailing new line) without any success.

Name: <%= @user.name %>
<%- if @user.phone.present? -%>
Phone: <%= @user.phone %>
<%- end -%>
Address: <%= @user.address -%>

How do I work around this issue?

PS: I am on Rails 2.3.8.

Note 1

Right now, I am working around the issue using ruby hackery.

Helper Method:

def display_fields(names, user)
  names.collect do |name| 
    value = user.send(name)
    "#{name}: #{value}" unless value.blank?
  end.compact.join("\n")
end

View code

<%= display_fields(["Name", "Phone", "Address"], @user) %>

But this looks quite clunky to me. I am interested in knowing if anybody has been able to get the <%--%> working in ERB view templates.

Harish Shetty
  • 64,083
  • 21
  • 152
  • 198

6 Answers6

31

To enable trim mode you have to instantiate the ERB object with '-' as the third parameter

ERB.new(template, nil, '-')
willmcneilly
  • 638
  • 8
  • 14
  • Oddly enough neither 1.8, nor 1.9 documentation says anything about the '-' option. It's there in the code though. Can anyone clarify this? 1.8: http://ruby-doc.org/stdlib-1.8.7/libdoc/erb/rdoc/ERB.html#method-c-new 1.9: http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html#method-c-new Source: http://ruby-doc.org/gems/docs/p/Pimki-1.8.200/ERB/Compiler.html#method-i-prepare_trim_mode – krukid Jan 09 '13 at 17:38
  • 2
    @krukid: 2.1 now says: http://ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html, but is not very precise on what it does. Related question: http://stackoverflow.com/questions/3801550/what-does-mean-in-ruby-on-rails-compared-to/25613037 – Ciro Santilli OurBigBook.com Sep 01 '14 at 21:27
18

I had to combine the answers by willmcneilly, RobinBrouwer and fbo.

enable trim mode

ERB.new(File.read(filename), nil, '-')

Change to -%>

<% $things.each do |thing| -%>
  <object name="<%= thing.name %>">
    <type><%= thing.name %></type>
  </object>
<% end -%>

And finally, convert from dos to unix. I used the following in Vim:

:set fileformat=unix
:w
Scymex
  • 954
  • 9
  • 17
5

According to the latest rails docs (http://guides.rubyonrails.org/v2.3.8/configuring.html#configuring-action-view):

ActionView::TemplateHandlers::ERB.erb_trim_mode gives the trim mode to be used by ERB. It defaults to '-'.

They reference the ERB docs (http://www.ruby-doc.org/stdlib-2.0.0/libdoc/erb/rdoc/ERB.html#method-c-new)

If trim_mode is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:
%  enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
>  omit newline for lines ending in %>
-  omit blank lines ending in -%>

So all you should need to do is have the dash in your closing erb tag like -%>. You may need to play with the trim mode if you are seeing unexpected results.

codenamev
  • 2,203
  • 18
  • 24
5

Try this:

Name: <%= @user.name %>
<% unless @user.phone.blank? -%>Phone: <%= @user.phone %><% end -%>
Address: <%= @user.address %>

Also, don't know if this will work:

Name: <%= @user.name %>
<%= "Phone: #{@user.phone}" if @user.phone.present? -%>
Address: <%= @user.address %>

If that doesn't work either, this should do the trick:

Name: <%= @user.name %><%= "\nPhone: #{@user.phone}" if @user.phone.present? %>
Address: <%= @user.address %>
RobinBrouwer
  • 973
  • 6
  • 13
  • 1
    Approach 1, 2 doesn't work. Variation of the 3rd approach is what I am using currently. Look at my updated question. Issue is more about why the `<%--%>` tags does not work.. – Harish Shetty Jan 09 '11 at 01:13
4

I had the same problem, it was due to space characters after %>.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
fbo
  • 41
  • 1
  • Yes, that's one way of losing line breaks - put all your erb `if`|`unless` [...] `end` statements in one line. I sometimes do this, but it isn't very readable for maintenance. – Dave Everitt Feb 24 '13 at 11:47
3

By using the '>' option, you will omit newlines for lines ending in %>

ERB.new(template, nil, '>')

That means you can wrap Ruby code inside <% %> tags, as usual. Unfortunately, I haven't found a way to remove the spaces before the starting <% tag.

Sebastian
  • 2,154
  • 1
  • 26
  • 42