-%> 之间的差异和%>在铁轨上 [英] Difference between -%> and %> in rails

查看:15
本文介绍了-%> 之间的差异和%>在铁轨上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了一些rails教程,并注意到一些视图代码块就像

<h1><%= @subject.name -%></h1>

和其他代码块一样

<h1><%= @subject.name %></h1>

-%> 和 %> 有什么区别

如果你知道一些好的语法参考,你可以指点我,那也会很有帮助.

解决方案

额外的破折号使ERB不输出结束标签后的换行符.你的例子没有什么区别,但是如果你有这样的东西:

<% 如果为真 -%>你好<%结束-%></div>

它会产生:

你好</div>

不是这个:

你好</div>

I have started some rails tutorials and noticed that some of the view code blocks are like

<h1><%= @subject.name -%></h1>

and other code blocks are like

<h1><%= @subject.name %></h1>

What is the difference between -%> and %>

If you know of some good syntax references you can point me to, that would also be helpful.

解决方案

The extra dash makes ERB not output the newline after the closing tag. There's no difference in your example, but if you have something like this:

<div>
  <% if true -%>
  Hi
  <% end -%>
</div>

It'll produce:

<div>
  Hi
</div>

and not this:

<div>

  Hi

</div>

这篇关于-%&gt; 之间的差异和%&gt;在铁轨上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆