新手问题 ERB 里面 `-%>` 是什么意思啊

blacktulip · August 04, 2013 · Last by luikore replied at August 05, 2013 · 7809 hits

在 github 看到这样的代码

<% if @details.errors.any? -%>
  <div id="error_explanation">
    <ul>
    <% @details.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end -%>

请问这个 ifend 后面的 -%> 是什么意思呢? 我开始觉得是手误,但是两处都有又不像...

忽略后面的空格和换行符,使输出更紧凑

#1 楼 @luikore 原来是这用法

#1 楼 @luikore thx,原来是这样

另外 <%= expr =%> 相当于 <%= expr -%>

参考 erubis 的文档

~/.rvm/gems/ruby-head/gems/erubis-2.7.0/doc/users-guide.html#topics-trimspaces

谢谢指点... 幸好没有去发 pr

#4 楼 @luikore 如果是像<%== expr -%>这样的呢,代码来源:https://github.com/citrus/spree_variant_options/blob/master/app/views/spree/products/_variant_options.html.erb 是用于删除前面的空白的吗

#7 楼 @Martin91 相当于 <%= raw expr -%>, erb 搞不了前导空白

9 Floor has deleted

#8 楼 @luikore sorry,找到了, http://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety

To insert something vzrbatim use the raw helper rather than calling html_safe:

<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>

or, equivalently, use <%==:

<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
Unknow user #11 August 04, 2013

原来如此,又涨姿势了……

老早就知道这个的作用,但是之前用起来却发现似乎没有效果...

#12 楼 @huacnlee erubis 默认支持的 erb 要设置 trim mode

$ erb -T -
<div><%= 1 -%> </div>
^D
<div>1</div>
You need to Sign in before reply, if you don't have an account, please Sign up first.