ERB模板删除尾随线

Har*_*tty 23 ruby-on-rails erb

我有一个用于发送电子邮件的ERB模板.

Name: <%= @user.name %>
<% if @user.phone.present? %>
Phone: <%= @user.phone %>
<% end %>
Address: <%= @user.address %>
Run Code Online (Sandbox Code Playgroud)

我试图删除之间的空白行NameAddress何时Phone为空.

返回结果

Name: John Miller 

Address: X124 Dummy Lane, Dummy City, CA
Run Code Online (Sandbox Code Playgroud)

预期结果

Name: John Miller 
Address: X124 Dummy Lane, Dummy City, CA
Run Code Online (Sandbox Code Playgroud)

我试图使用<%--%>标签(删除尾随的新行)没有任何成功.

Name: <%= @user.name %>
<%- if @user.phone.present? -%>
Phone: <%= @user.phone %>
<%- end -%>
Address: <%= @user.address -%>
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

PS:我在Rails 2.3.8上.

注1

现在,我正在使用ruby hackery解决这个问题.

助手方法:

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

查看代码

<%= display_fields(["Name", "Phone", "Address"], @user) %>
Run Code Online (Sandbox Code Playgroud)

但这看起来很笨重.我有兴趣知道是否有人能够<%--%>在ERB视图模板中工作.

wil*_*lly 26

要启用修剪模式,必须使用" - "作为第三个参数来实例化ERB对象

ERB.new(template, nil, '-')
Run Code Online (Sandbox Code Playgroud)

  • @krukid:2.1现在说:http://ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html,但对它的作用不是很精确.相关问题:http://stackoverflow.com/questions/3801550/what-does-mean-in-ruby-on-rails-compared-to/25613037 (2认同)

Scy*_*mex 11

我不得不将willmcneilly,RobinBrouwer和fbo的答案结合起来.

启用修剪模式

ERB.new(File.read(filename), nil, '-')
Run Code Online (Sandbox Code Playgroud)

更改为 - %>

<% $things.each do |thing| -%>
  <object name="<%= thing.name %>">
    <type><%= thing.name %></type>
  </object>
<% end -%>
Run Code Online (Sandbox Code Playgroud)

最后,从dos转换为unix.我在Vim中使用了以下内容:

:set fileformat=unix
:w
Run Code Online (Sandbox Code Playgroud)


Rob*_*wer 5

试试这个:

Name: <%= @user.name %>
<% unless @user.phone.blank? -%>Phone: <%= @user.phone %><% end -%>
Address: <%= @user.address %>
Run Code Online (Sandbox Code Playgroud)

另外,不知道这是否有效:

Name: <%= @user.name %>
<%= "Phone: #{@user.phone}" if @user.phone.present? -%>
Address: <%= @user.address %>
Run Code Online (Sandbox Code Playgroud)

如果这也不起作用,这应该可以解决问题:

Name: <%= @user.name %><%= "\nPhone: #{@user.phone}" if @user.phone.present? %>
Address: <%= @user.address %>
Run Code Online (Sandbox Code Playgroud)


cod*_*mev 5

根据最新的rails docs(http://guides.rubyonrails.org/v2.3.8/configuring.html#configuring-action-view):

ActionView :: TemplateHandlers :: ERB.erb_trim_mode给出了ERB使用的修剪模式.它默认为' - '.

他们参考了ERB文档(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 -%>
Run Code Online (Sandbox Code Playgroud)

所以你需要做的就是在关闭erb标签中使用破折号-%>.如果您看到意外结果,则可能需要使用修剪模式.