【问题标题】:ERB Template removing the trailing lineERB 模板删除尾随行
【发布时间】:2011-06-05 16:41:31
【问题描述】:

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

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

Phone 为空时,我正在尝试删除NameAddress 之间的空白行。

返回结果

Name: John Miller 

Address: X124 Dummy Lane, Dummy City, CA

预期结果

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

我尝试使用&lt;%--%&gt; 标签(删除尾随的新行)但没有成功。

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

我该如何解决这个问题?

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

查看代码

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

但这对我来说看起来很笨重。我很想知道是否有人能够让&lt;%--%&gt; 在 ERB 视图模板中工作。

【问题讨论】:

    标签: ruby-on-rails erb


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    我必须结合 willmcneilly、RobinBrouwer 和 fbo 的答案。

    启用修剪模式

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

    改为-%>

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

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

    :set fileformat=unix
    :w
    

    【讨论】:

      【解决方案3】:

      试试这个:

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

      另外,不知道这是否可行:

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

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

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

      【讨论】:

      • 方法 1、2 不起作用。第三种方法的变化是我目前使用的。看看我更新的问题。问题更多是关于为什么&lt;%--%&gt; 标签不起作用..
      【解决方案4】:

      根据最新的 Rails 文档 (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 -%>
      

      因此,您需要做的就是在结束 erb 标记中添加破折号,例如 -%&gt;。如果您看到意想不到的结果,您可能需要使用修剪模式。

      【讨论】:

        【解决方案5】:

        我也遇到了同样的问题,是%&gt;后面的空格造成的。

        【讨论】:

        • 是的,这是丢失换行符的一种方式 - 将所有 erb if|unless [...] end 语句放在一行中。我有时会这样做,但它对维护的可读性不是很好。
        【解决方案6】:

        通过使用 '>' 选项,您将省略以 %> 结尾的行的换行符

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

        这意味着您可以像往常一样将 Ruby 代码包装在 标记中。 不幸的是,我还没有找到删除起始

        【讨论】:

          猜你喜欢
          • 2017-03-01
          • 1970-01-01
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-06
          • 2011-05-30
          • 1970-01-01
          • 2019-09-18
          相关资源
          最近更新 更多