<%、<%=、<%# 和 -%> 之间有什么区别?在Rails 中的ERB 中?
有人可以描述一下 ERB 文件中使用的以下字符的用法吗:
<% %>
<%= %>
<% -%>
<%# %>
每个字符的用法是什么?
Can some one please describe the usage of the following characters which is used in ERB file:
<% %>
<%= %>
<% -%>
<%# %>
what's the usage of each one ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
执行括号内的 ruby 代码。
将某些内容打印到 erb 文件中。
相当于
<%= raw %>
。将一些内容逐字打印(即不转义)到 erb 文件中。 (摘自 Ruby on Rails 指南。)避免表达式后换行。
注释掉括号内的代码;不发送给客户端(与 HTML 注释相反)。
有关 ERB 的更多信息,请访问 Ruby Doc。
Executes the ruby code within the brackets.
Prints something into erb file.
Equivalent to
<%= raw %>
. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)Avoids line break after expression.
Comments out code within brackets; not sent to client (as opposed to HTML comments).
Visit Ruby Doc for more infos about ERB.
<% %>
和<%- and -%>
适用于任何 Ruby 代码,但不输出结果(例如 if 语句)。两者是相同的。<%= %>
用于输出 Ruby 代码的结果<%# %>
是 ERB 注释这里有一个很好的指南:
http://api.rubyonrails.org/classes/ActionView/Base.html
<% %>
and<%- and -%>
are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.<%= %>
is for outputting the results of Ruby code<%# %>
is an ERB commentHere's a good guide:
http://api.rubyonrails.org/classes/ActionView/Base.html
Rails 不使用stdlib ERB 默认情况下,它使用 erubis。资料来源:此开发者的评论、ActionView 的 gemspec, 接受了我在编写本文时所做的合并请求。
它们之间存在行为差异,特别是连字符运算符
%-
和-%
的工作方式。文档很少,Ruby 的 ERB 格式“官方”定义在哪里?以下是经验结论。
所有测试假设:
当您可以使用
-
-
传递给的
trim_mode
选项ERB.new 来使用它。示例:
-%
的作用:ERB:如果是换行符,则删除下一个字符。
埃鲁比斯:
在
<% %>
中(没有=
),-
没有用,因为<% % >
和<% -%>
相同。<% %>
如果当前行仅包含空格,则删除当前行,否则不执行任何操作。在
<%= -%>
中(使用=
):操作 示例:
%-
的作用:ERB:删除标签前的空格以及之前的换行符之后,但前提是之前只有空格。
erubis:无用,因为
<%- %>
与<% %>
相同(没有=
),并且不能与=
一起使用,这是-%
有用的唯一情况。所以永远不要使用这个。示例:
%-
和-%
一起做什么两种效果的精确组合。
Rails does not use the stdlib's ERB by default, it uses erubis. Sources: this dev's comment, ActionView's gemspec, accepted merge request I did while writing this.
There are behavior differences between them, in particular on how the hyphen operators
%-
and-%
work.Documentation is scarce, Where is Ruby's ERB format "officially" defined? so what follows are empirical conclusions.
All tests suppose:
When you can use
-
-
totrim_mode
option ofERB.new
to use it.Examples:
What
-%
does:ERB: remove the next character if it is a newline.
erubis:
in
<% %>
(without=
),-
is useless because<% %>
and<% -%>
are the same.<% %>
removes the current line if it only contains whitespaces, and does nothing otherwise.in
<%= -%>
(with=
):Examples:
What
%-
does:ERB: remove whitespaces before tag and after previous newlines, but only if there are only whitespaces before.
erubis: useless because
<%- %>
is the same as<% %>
(without=
), and this cannot be used with=
which is the only case where-%
can be useful. So never use this.Examples:
What
%-
and-%
do togetherThe exact combination of both effects separately.
<% %> :执行 ruby 代码
<%= %>
:打印到 Erb 文件中。或浏览器<% -%>
:避免表达式后换行。<%# %>
:ERB 评论<% %>
: Executes the ruby code<%= %>
: Prints into Erb file. Or browser<% -%>
: Avoids line break after expression.<%# %>
: ERB comment由于其晦涩难懂,我添加了
<%%
文字标记分隔符作为对此的答案。这将告诉 erb 不要解释标签的<%
部分,这对于 js 应用程序(如显示 Chart.js 工具提示等)是必需的。更新(修复了损坏的链接)
一切关于 ERB 现在可以在这里找到:
https://puppet.com/docs/puppet/5.3/lang_template_erb.html#标签
I've added the
<%%
literal tag delimiter as an answer to this because of its obscurity. This will tell erb not to interpret the<%
part of the tag which is necessary for js apps like displaying chart.js tooltips etc.Update (Fixed broken link)
Everything about ERB can now be found here:
https://puppet.com/docs/puppet/5.3/lang_template_erb.html#tags
这些在 ruby on Rails 中使用:-
<% %> :-
<% %>;标签用于执行不返回任何内容的 Ruby 代码,例如条件、循环或块。例如:-
<%= %> :-
用于显示内容。
<% -%>:-
Rails 扩展了 ERB,因此您只需在 Rails 模板中的标记中添加尾随连字符即可抑制换行符
<%# %>:-
注释掉代码
These are use in ruby on rails :-
<% %> :-
The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-
<%= %> :-
use to display the content .
<% -%>:-
Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates
<%# %>:-
comment out the code
<% %>
执行其中的代码但不打印结果,例如:我们可以在 erb 文件中将它用于 if else 。
将打印
temp is 1
<%= %>
执行代码并打印输出,例如:我们可以打印 Rails 变量的值。
将打印
1
<% -%>
这没有什么区别,因为它不打印任何内容,-%>
仅对以下内容有意义<%= -%>
,这将避免换行。<%# %>
将注释掉其中编写的代码。<% %>
executes the code in there but does not print the result, for eg:We can use it for if else in an erb file.
Will print
temp is 1
<%= %>
executes the code and also prints the output, for eg:We can print the value of a rails variable.
Will print
1
<% -%>
It makes no difference as it does not print anything,-%>
only makes sense with<%= -%>
, this will avoid a new line.<%# %>
will comment out the code written within this.