ERB Template removing the trailing line

2019-04-03 14:57发布

问题:

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.

回答1:

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

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


回答2:

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


回答3:

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


回答4:

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.



回答5:

I had the same problem,

it was due to space characters afer %>

Maybe it will help you

François



回答6:

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.