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

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

问题描述

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

<%= @subject.name -%>

和其他代码块一样

<%= @subject.name %>

-%> 和 %> 有什么区别

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

解决方案

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

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

它会产生:

你好

而不是这个:

你好

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;之间的差异和%>在轨道上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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