什么时候使用而不是

时间:2022-07-26 21:04:42

I've noticed that in some lines of rails views, this is used:

我注意到在某些rails视图中,使用了以下内容:

<% # Code... -%> 

instead of:

<% # Code... %>

What is the difference?

有什么不同?

4 个解决方案

#1


17  

    <ul>
    <% @posts.each do |post| -%> 
      <li><%=post.title%></li>
    <% end -%>
    </ul>

There will be no new lines in between the <ul> and first <li> and the last closing </li> and </ul>. If the - was omitted, there would.

    和第一个
  • 以及最后一个结束 和 之间不会有新的行。如果 - 被省略,那就有了。

  • 以及最后一个结束和之间不会有新的行。如果 - 被省略,那就有了。

#2


8  

The different options for evaluating code in ERB are as follows (they can be accessed in Textmate using Ctrl-Shift-. ):

在ERB中评估代码的不同选项如下(可以使用Ctrl-Shift-在Textmate中访问它们):

  • <% %> Just evaluate the contents.
  • <%%>只评估内容。

  • <%= %> Evaluate the contents and puts the result.
  • <%=%>评估内容并输出结果。

  • <%= -%> Evaluate the contents and prints the result.
  • <%= - %>评估内容并打印结果。

  • <%# %> The contents is treated as a comment and not outputted.
  • <%#%>内容被视为注释而不输出。

Notice the difference between puts and print. Puts always adds a new line at the end of the string whereas print doesnt.

注意puts和print之间的区别。 Puts总是在字符串的末尾添加一个新行,而print不会。

Basically, the -%> says don't output a new line at the end.

基本上, - %>表示最后不输出新行。

#3


2  

Consider this

<div>
    <% if @some_var == some_value %>
    <p>Some message</p>
    <% end %>
</div>

The code above yields to the HTML below if the @some_var is some_value

如果@some_var是some_value,则上面的代码会产生HTML

<div>

    <p>Some message</p>

</div>

If you've put - in the closing tag, then the ERB interpreter would remove the new lines for those with code tag including - and result in the following

如果你把 - 在结束标记中,那么ERB解释器将为包含代码标记的那些删除新行 - 并导致以下结果

<div>       
    <p>Some message</p>        
</div>

This is useful if you need to have a good looking code for HTML. Sometimes you'll find it useful when working sideby side with a designer

如果您需要具有良好的HTML代码,这将非常有用。有时,当你与设计师并肩工作时,你会发现它很有用

Hope this helps.

希望这可以帮助。

#4


1  

A little late, but I think it's worth pointing out that you can also do this:

有点晚了,但我认为值得指出你也可以这样做:

<%- @posts.each do |post| -%>
  <li><%= post.title %></li>
<%- end %>

This strips away any whitespace in front.

这剥夺了前面的任何空白。

#1


17  

    <ul>
    <% @posts.each do |post| -%> 
      <li><%=post.title%></li>
    <% end -%>
    </ul>

There will be no new lines in between the <ul> and first <li> and the last closing </li> and </ul>. If the - was omitted, there would.

    和第一个
  • 以及最后一个结束 和 之间不会有新的行。如果 - 被省略,那就有了。

  • 以及最后一个结束和之间不会有新的行。如果 - 被省略,那就有了。

#2


8  

The different options for evaluating code in ERB are as follows (they can be accessed in Textmate using Ctrl-Shift-. ):

在ERB中评估代码的不同选项如下(可以使用Ctrl-Shift-在Textmate中访问它们):

  • <% %> Just evaluate the contents.
  • <%%>只评估内容。

  • <%= %> Evaluate the contents and puts the result.
  • <%=%>评估内容并输出结果。

  • <%= -%> Evaluate the contents and prints the result.
  • <%= - %>评估内容并打印结果。

  • <%# %> The contents is treated as a comment and not outputted.
  • <%#%>内容被视为注释而不输出。

Notice the difference between puts and print. Puts always adds a new line at the end of the string whereas print doesnt.

注意puts和print之间的区别。 Puts总是在字符串的末尾添加一个新行,而print不会。

Basically, the -%> says don't output a new line at the end.

基本上, - %>表示最后不输出新行。

#3


2  

Consider this

<div>
    <% if @some_var == some_value %>
    <p>Some message</p>
    <% end %>
</div>

The code above yields to the HTML below if the @some_var is some_value

如果@some_var是some_value,则上面的代码会产生HTML

<div>

    <p>Some message</p>

</div>

If you've put - in the closing tag, then the ERB interpreter would remove the new lines for those with code tag including - and result in the following

如果你把 - 在结束标记中,那么ERB解释器将为包含代码标记的那些删除新行 - 并导致以下结果

<div>       
    <p>Some message</p>        
</div>

This is useful if you need to have a good looking code for HTML. Sometimes you'll find it useful when working sideby side with a designer

如果您需要具有良好的HTML代码,这将非常有用。有时,当你与设计师并肩工作时,你会发现它很有用

Hope this helps.

希望这可以帮助。

#4


1  

A little late, but I think it's worth pointing out that you can also do this:

有点晚了,但我认为值得指出你也可以这样做:

<%- @posts.each do |post| -%>
  <li><%= post.title %></li>
<%- end %>

This strips away any whitespace in front.

这剥夺了前面的任何空白。