有人可以描述ERB文件中使用的以下字符的用法:
<% %>
<%= %>
<% -%>
<%# %>
每个人的用法是什么?
<% %>
在括号内执行ruby代码。
<%= %>
将某些内容打印到erb文件中。
<% -%>
表达后避免换行。
<%# %>
注释括号内的代码;没有发送到客户端(而不是HTML注释)。
访问Ruby Doc获取有关ERB的更多信息。
<% %>
和<%- and -%>
适用于任何Ruby代码,但不输出结果(例如if语句)。两者是一样的。
<%= %>
用于输出Ruby代码的结果
<%# %>
是ERB的评论
这是一个很好的指南:http://api.rubyonrails.org/classes/ActionView/Base.html
Rails默认不使用stdlib's ERB,它使用erubis。资料来源:this dev's comment,ActionView's gemspec,accepted merge request I did while writing this。
它们之间存在行为差异,特别是关于连字符运算符%-
和-%
如何工作。
文档很少,Where is Ruby's ERB format "officially" defined?所以接下来是实证结论。
所有测试都假设:
require 'erb'
require 'erubis'
什么时候可以使用-
-
传递给trim_mode
的ERB.new
选项才能使用它。例子:
begin ERB.new("<%= 'a' -%>\nb").result; rescue SyntaxError ; else raise; end
ERB.new("<%= 'a' -%>\nb" , nil, '-') .result == 'ab' or raise
Erubis::Eruby.new("<%= 'a' -%> \n b").result == 'a b' or raise
什么-%
做:
<% %>
(没有=
),-
是无用的,因为<% %>
和<% -%>
是相同的。如果<% %>
只包含空格,则删除当前行,否则不执行任何操作。
在<%= -%>
(与=
):
如果它只包含空格,则删除整行
否则,如果标签前面有非空格,并且只有whitesapces之后,请删除之后的空白
否则,标签后面有一个非空格:什么都不做例子:
# Remove
ERB.new("a \nb <% 0 -%>\n c", nil, '-').result == "a \nb c" or raise
# Don't do anything: not followed by newline, but by space:
ERB.new("a\n<% 0 -%> \nc", nil, '-').result == "a\nb \nc" or raise
# Remove the current line because only whitesapaces:
Erubis::Eruby.new(" <% 0 %> \nb").result == 'b' or raise
# Same as above, thus useless because longer.
Erubis::Eruby.new(" <% 0 -%> \nb").result == 'b' or raise
# Don't do anything because line not empty.
Erubis::Eruby.new("a <% 0 %> \nb").result == "a \nb" or raise
Erubis::Eruby.new(" <% 0 %> a\nb").result == " a\nb" or raise
Erubis::Eruby.new(" <% 0 -%> a\nb").result == " a\nb" or raise
# Don't remove the current line because of `=`:
Erubis::Eruby.new(" <%= 0 %> \nb").result == " 0 \nb" or raise
# Remove the current line even with `=`:
Erubis::Eruby.new(" <%= 0 -%> \nb").result == " 0b" or raise
# Remove forward only because of `-` and non space before:
Erubis::Eruby.new("a <%= 0 -%> \nb").result == "a 0b" or raise
# Don't do anything because non-whitespace forward:
Erubis::Eruby.new(" <%= 0 -%> a\nb").result == " 0 a\nb" or raise
什么%-
做:
<%- %>
与<% %>
(没有=
)相同,并且这不能与=
一起使用,-%
是唯一可以使用# Remove
ERB.new("a \n <%- 0 %> b\n c", nil, '-').result == "a \n b\n c" or raise
# b is not whitespace: do nothing:
ERB.new("a \nb <%- 0 %> c\n d", nil, '-').result == "a \nb c\n d" or raise
的情况。所以永远不要使用它。例子:
%-
什么-%
和<%%
一起做
两种效果的确切组合分开。
我添加了<%
文字标记分隔符作为答案,因为它的含糊不清。这将告诉erb不要解释标签的https://puppet.com/docs/puppet/5.3/lang_template_erb.html#tags部分,这对js应用程序来说是必要的,例如显示chart.js工具提示等。
更新(修复损坏的链接)
关于ERB的一切现在都可以在这里找到:<% %>
<%= %>
:执行ruby代码<% -%>
:打印到Erb文件。或浏览器<%# %>
:表达后避免换行。<h1>Names of all the people</h1>
<% @people.each do |person| %>
Name: <%= person.name %><br>
<% end %>
:ERB评论这些用于轨道上的红宝石: -
<% %> :-
<%%>标记用于执行不返回任何内容的Ruby代码,例如条件,循环或块。例如: -
Name: <%= person.name %><br>
<%= %> :-
用于显示内容。
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
<% -%>:-
Rails扩展了ERB,因此您只需在Rails模板中为标记添加尾部连字符即可抑制换行符
<%# %>:-
注释掉代码
<% %>
<% temp = 1 %>
<% if temp == 1%>
temp is 1
<% else %>
temp is not 1
<%end%>
在那里执行代码但不打印结果,例如:
我们可以在erb文件中将其用于if else。
temp is 1
将打印<%= %>
<% temp = 1 %>
<%= temp %>
执行代码并打印输出,例如:
我们可以打印rails变量的值。
1
将打印<% -%>
-%>
它没有任何区别,因为它没有打印任何东西,<%= -%>
只对<%# %>
有意义,这将避免新的一行。
qazxswpoi将注释掉这里写的代码。