<%、<%=、<%#和-%>之间的区别是什么?在Rails中的ERB中?
问题描述:
有人可以描述一下ERB文件中使用的以下字符的用法:
Can some one please describe the usage of the following characters which is used in ERB file:
<% %>
<%= %>
<% -%>
<%# %>
每一个的用途是什么?
答
<% %>
执行括号内的 ruby 代码.
Executes the ruby code within the brackets.
<%= %>
将某些内容打印到 erb 文件中.
Prints something into erb file.
<%== %>
相当于 .将某些内容逐字打印(即没有转义)到 erb 文件中.(摘自 Ruby on Rails 指南.)
Equivalent to <%= raw %>
. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)
<% -%>
避免在表达式后换行.
<%# %>
注释掉括号内的代码;不发送给客户端(与 HTML 注释相反).
Comments out code within brackets; not sent to client (as opposed to HTML comments).
访问 Ruby 文档了解更多信息关于ERB.
Visit Ruby Doc for more infos about ERB.