Ruby-on-rails 删除尾随行的 ERB 模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERB Template removing the trailing line
提问by Harish Shetty
I have an ERB template for sending an email.
我有一个用于发送电子邮件的 ERB 模板。
Name: <%= @user.name %>
<% if @user.phone.present? %>
Phone: <%= @user.phone %>
<% end %>
Address: <%= @user.address %>
I am trying to remove the blank line between Nameand Addresswhen Phoneis empty.
我正在尝试删除空行Name和Address何时Phone为空。
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.
PS:我在 Rails 2.3.8 上。
Note 1
注 1
Right now, I am working around the issue using ruby hackery.
现在,我正在使用 rubyhackery 解决这个问题。
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.
但这对我来说看起来很笨重。我很想知道是否有人能够<%--%>在 ERB 视图模板中工作。
回答by willmcneilly
To enable trim mode you have to instantiate the ERB object with '-' as the third parameter
要启用修剪模式,您必须使用“-”作为第三个参数实例化 ERB 对象
ERB.new(template, nil, '-')
回答by Scymex
I had to combine the answers by willmcneilly, RobinBrouwer and fbo.
我不得不结合 willmcneilly、RobinBrouwer 和 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:
最后,从 dos 转换到 unix。我在 Vim 中使用了以下内容:
:set fileformat=unix
:w
回答by RobinBrouwer
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 %>
回答by codenamev
According to the latest rails docs (http://guides.rubyonrails.org/v2.3.8/configuring.html#configuring-action-view):
根据最新的 Rails 文档(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 '-'.
ActionView::TemplateHandlers::ERB.erb_trim_mode 给出了 ERB 使用的修剪模式。它默认为“-”。
They reference the ERB docs (http://www.ruby-doc.org/stdlib-2.0.0/libdoc/erb/rdoc/ERB.html#method-c-new)
他们引用了 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 -%>
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.
因此,您需要做的就是在结束 erb 标签中添加破折号,例如-%>. 如果您看到意外结果,您可能需要使用修剪模式。
回答by fbo
I had the same problem, it was due to space characters after %>.
我遇到了同样的问题,这是由于%>.
回答by Sebastian
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.
这意味着您可以像往常一样将 Ruby 代码包装在 <% %> 标签中。不幸的是,我还没有找到一种方法来删除起始 <% 标记之前的空格。

