Ruby on rails <;%<;%=<;%#和-%>;雇员再培训局的铁路?

Ruby on rails <;%<;%=<;%#和-%>;雇员再培训局的铁路?,ruby-on-rails,ruby,templates,erb,Ruby On Rails,Ruby,Templates,Erb,请说明ERB文件中使用的以下字符的用法: <% %> <%= %> <% -%> <%# %> 每一个的用途是什么 <% %> 执行括号内的ruby代码 <%= %> 将某些内容打印到erb文件中 <%== %> 相当于。逐字打印(即不转义)到erb文件中。(摘自) 避免表达式后的换行 <%# %> 注释括号内的代码;未发送到客户端(与HTML注释相反) 有

请说明ERB文件中使用的以下字符的用法:

<%   %>
<%=  %>
<%  -%>
<%#  %>

每一个的用途是什么


<% %>
执行括号内的ruby代码

<%= %>

将某些内容打印到erb文件中

<%== %>

相当于
。逐字打印(即不转义)到erb文件中。(摘自)


避免表达式后的换行

<%# %>

注释括号内的代码;未发送到客户端(与HTML注释相反)

有关ERB的更多信息,请访问。

适用于任何Ruby代码,但不输出结果(例如if语句)。两者是一样的

用于输出Ruby代码的结果

是雇员再培训局的评论

这里有一个很好的指南:
Rails不使用默认情况下使用的。资料来源:

它们之间存在行为差异,特别是连字符操作符
%-
-%
的工作方式

文献资料很少,所以下面是实证结论

所有测试都假设:

require 'erb'
require 'erubis'
何时可以使用
-

  • ERB:您必须将
    -
    传递到
    ERB的
    微调模式
    选项。新建
    才能使用它
  • erubis:默认情况下启用
示例:

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
# 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:删除标记前面和前面的换行符后面的空格,但前提是前面只有空格

  • erubis:无用,因为
    相同(没有
    =
    ),这不能与
    =
    一起使用,这是
    -%
    唯一有用的情况。所以永远不要用这个

示例:

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
# 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.new(“a\n b\n c”,nil,'-')。结果==“a\n b\n c”或raise
#b不是空白:什么也不做:
ERB.new(“a\nb c\n d”,nil,'-')。结果==“a\nb c\n d”或raise
什么
%-
-%
一起做


这两种效果的精确组合是分开的

我添加了在ruby on rails中使用的
:-

:-

这些标记用于执行不返回任何内容(例如条件、循环或块)的Ruby代码。例如:-

<h1>Names of all the people</h1>
<% @people.each do |person| %>
  Name: <%= person.name %><br>
<% end %>
所有人的名字
名称:
:-

用于显示内容

Name: <%= person.name %><br>
名称:
:-

Rails扩展了ERB,因此只需在Rails模板中的标记中添加一个尾随连字符,就可以抑制换行符

:-

注释掉代码

<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>

你好,史密斯先生。
执行其中的代码,但不打印结果,例如:
我们可以将其用于erb文件中的if-else

<% temp = 1 %>
<% if temp == 1%>
  temp is 1
<% else %>
  temp is not 1
<%end%>  
将打印
1


它没有任何区别,因为它不打印任何内容,
-%>
只在
时才有意义,这将避免出现新行


将注释掉其中编写的代码。

  • :执行ruby代码
  • :打印到Erb文件中。或浏览器
  • :避免表达式后换行
  • :雇员再培训局评论

这里有一些很好的答案。还要注意的是,Haml通常是ERb的一个更好的替代品。是的,它比ERb更容易处理
-%>
@Ciro Santilli的可能副本:尽管年龄大,但这一个似乎比你的参考资料更完整。我投票将它们作为这一个的副本关闭。
是不同的:只有后者删除了尾随空格。我在代码中遇到过这种变体,其他人知道它是什么吗?双等号意味着字符串不会像原始字符串那样被转义。Rails不使用stdlib ERB:它使用erubis:但是。。。但是<代码>@QPaysTaxes很抱歉,你说得对。。。例如,如果您有多行ERB
注释掉所有内容,如果它只是一条普通注释(即,它只注释掉它所在的行),则不会这样做。我看不到在这个答案(ActionView::Base docs页面)中引用的页面中打开
@AndrewVit-:抑制前导和尾随空格,包括尾随换行符,并可与互换使用。“如何抑制的前导空格?在您写的第二行中,Rails在链接到erubis时使用了eruby。@DanielJonsson感谢您的报告:它本应该是erubis。有这么多版本,真是疯了。更新。此链接准确回答OP链接现在断开使用新链接更新!