Can some one please describe the usage of the following characters which is used in ERB file:
请描述一下在ERB文件中使用的下列字符的用法:
<% %>
<%= %>
<% -%>
<%# %>
what's the usage of each one ?
每一个的用法是什么?
5 个解决方案
#1
378
<% %>
Executes the ruby code within the brackets.
在括号内执行ruby代码。
<%= %>
Prints something into erb file.
在erb文件中打印一些东西。
<% -%>
Avoids line break after expression.
在表达式之后避免换行。
<%# %>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
注释在括号内的代码;不发送给客户端(与HTML注释相反)。
Visit Ruby Doc for more infos about ERB.
访问Ruby Doc以获得更多关于ERB的信息。
#2
79
<% %>
and <%- and -%>
are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.
<% %>和<%-和-%>是任何Ruby代码,但不输出结果(如if语句)。两者是一样的。
<%= %>
is for outputting the results of Ruby code
<%= %>用于输出Ruby代码的结果。
<%# %>
is an ERB comment
<%# %>是一个ERB评论。
Here's a good guide: http://api.rubyonrails.org/classes/ActionView/Base.html
这里有一个很好的指南:http://api.rubyonrails.org/classes/ActionView/Base.html。
#3
39
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.
Rails在默认情况下不使用stdlib的ERB,它使用了erubis。来源:这个dev的评论,ActionView的gemspec,接受合并请求我在写这个的时候做了。
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.
文档很少,Ruby的ERB格式“正式”定义在哪里?下面是经验结论。
All tests suppose:
所有测试假设:
require 'erb'
require 'erubis'
When you can use -
当你可以使用的时候。
- ERB: you must pass
-
totrim_mode
option ofERB.new
to use it. - ERB:你必须通过ERB的trim_mode选项。新使用它。
- erubis: enabled by default.
- erubis:默认启用。
Examples:
例子:
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
What -%
does:
什么- - - - - - %:
-
ERB: remove the next character if it is a newline.
删除下一个字符,如果它是一条新行。
-
erubis:
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=
):在<%= -%> (with =):
- remove the entire line if it only contains whitespaces
- 删除整个行,如果它只包含空白。
- else, if there is a non-space before the tag, and only whitesapces after, remove the whitespces that come after
- 否则,如果在标签之前有一个非空格,然后只有白色的,移除后面的空白。
- else, there is a non-space after the tag: do nothing
- 另外,标签后面还有一个非空间:什么都不做。
-
Examples:
例子:
# 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
What %-
does:
什么% -:
-
ERB: remove whitespaces before tag and after previous newlines, but only if there are only whitespaces before.
ERB:在标签之前和之前的换行之前删除空白,但前提是之前只有空白。
-
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:
例子:
# 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
What %-
and -%
do together
%和-%一起做!
The exact combination of both effects separately.
两种效果的确切结合。
#4
5
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.
由于它的晦涩,我添加了<%% literal标记分隔符作为这个答案。这将告诉erb不要解释标签的<%部分,这是js应用程序如显示图表所必需的。js工具提示等。
Everything about ERB can be found here: https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html#tags
所有关于ERB的内容都可以在这里找到:https://docs.puppet/latest/reference/lang_template_erb.html #标签。
#5
1
These are use in ruby on rails :-
这些在ruby on rails中使用:-。
<% %> :-
< % % >:-
The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-
<% %>标记用于执行不返回任何内容的Ruby代码,例如条件、循环或块。如:-
<h1>Names of all the people</h1>
<% @people.each do |person| %>
Name: <%= person.name %><br>
<% end %>
<%= %> :-
< % = % >:-
use to display the content .
用于显示内容。
Name: <%= person.name %><br>
<% -%>:-
< %—% >:-
Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates
Rails扩展了ERB,这样您就可以简单地通过在Rails模板中添加一个拖尾连字符来抑制换行。
<%# %>:-
< % # % >:-
comment out the code
注释掉的代码
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
#1
378
<% %>
Executes the ruby code within the brackets.
在括号内执行ruby代码。
<%= %>
Prints something into erb file.
在erb文件中打印一些东西。
<% -%>
Avoids line break after expression.
在表达式之后避免换行。
<%# %>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
注释在括号内的代码;不发送给客户端(与HTML注释相反)。
Visit Ruby Doc for more infos about ERB.
访问Ruby Doc以获得更多关于ERB的信息。
#2
79
<% %>
and <%- and -%>
are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.
<% %>和<%-和-%>是任何Ruby代码,但不输出结果(如if语句)。两者是一样的。
<%= %>
is for outputting the results of Ruby code
<%= %>用于输出Ruby代码的结果。
<%# %>
is an ERB comment
<%# %>是一个ERB评论。
Here's a good guide: http://api.rubyonrails.org/classes/ActionView/Base.html
这里有一个很好的指南:http://api.rubyonrails.org/classes/ActionView/Base.html。
#3
39
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.
Rails在默认情况下不使用stdlib的ERB,它使用了erubis。来源:这个dev的评论,ActionView的gemspec,接受合并请求我在写这个的时候做了。
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.
文档很少,Ruby的ERB格式“正式”定义在哪里?下面是经验结论。
All tests suppose:
所有测试假设:
require 'erb'
require 'erubis'
When you can use -
当你可以使用的时候。
- ERB: you must pass
-
totrim_mode
option ofERB.new
to use it. - ERB:你必须通过ERB的trim_mode选项。新使用它。
- erubis: enabled by default.
- erubis:默认启用。
Examples:
例子:
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
What -%
does:
什么- - - - - - %:
-
ERB: remove the next character if it is a newline.
删除下一个字符,如果它是一条新行。
-
erubis:
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=
):在<%= -%> (with =):
- remove the entire line if it only contains whitespaces
- 删除整个行,如果它只包含空白。
- else, if there is a non-space before the tag, and only whitesapces after, remove the whitespces that come after
- 否则,如果在标签之前有一个非空格,然后只有白色的,移除后面的空白。
- else, there is a non-space after the tag: do nothing
- 另外,标签后面还有一个非空间:什么都不做。
-
Examples:
例子:
# 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
What %-
does:
什么% -:
-
ERB: remove whitespaces before tag and after previous newlines, but only if there are only whitespaces before.
ERB:在标签之前和之前的换行之前删除空白,但前提是之前只有空白。
-
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:
例子:
# 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
What %-
and -%
do together
%和-%一起做!
The exact combination of both effects separately.
两种效果的确切结合。
#4
5
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.
由于它的晦涩,我添加了<%% literal标记分隔符作为这个答案。这将告诉erb不要解释标签的<%部分,这是js应用程序如显示图表所必需的。js工具提示等。
Everything about ERB can be found here: https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html#tags
所有关于ERB的内容都可以在这里找到:https://docs.puppet/latest/reference/lang_template_erb.html #标签。
#5
1
These are use in ruby on rails :-
这些在ruby on rails中使用:-。
<% %> :-
< % % >:-
The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-
<% %>标记用于执行不返回任何内容的Ruby代码,例如条件、循环或块。如:-
<h1>Names of all the people</h1>
<% @people.each do |person| %>
Name: <%= person.name %><br>
<% end %>
<%= %> :-
< % = % >:-
use to display the content .
用于显示内容。
Name: <%= person.name %><br>
<% -%>:-
< %—% >:-
Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates
Rails扩展了ERB,这样您就可以简单地通过在Rails模板中添加一个拖尾连字符来抑制换行。
<%# %>:-
< % # % >:-
comment out the code
注释掉的代码
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>