首页 文章

Rails中ERB中的<%,<%=,<%#和 - %>有什么区别?

提问于
浏览
316

有人可以描述ERB文件中使用的以下字符的用法:

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

每个人的用法是什么?

7 回答

  • 44
    • <% %> :执行ruby代码

    • <%= %> :打印到Erb文件中 . 或浏览器

    • <% -%> :表达后避免换行 .

    • <%# %> :ERB评论

  • 414

    我添加了 <%% 文字标记分隔符作为答案,因为它的含糊不清 . 这将告诉erb不要解释标签的 <% 部分,这对js应用程序来说是必要的,例如显示chart.js工具提示等 .

    关于ERB的一切都可以在这里找到:https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html#tags

  • 4

    These are use in 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" %>
    
  • 83

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

    <%= %> 用于输出Ruby代码的结果

    <%# %> 是ERB评论

    这是一个很好的指南:http://api.rubyonrails.org/classes/ActionView/Base.html

  • 5

    Rails确实 not 默认使用stdlib's ERB,它使用erubis . 来源:this dev's commentActionView's gemspecaccepted merge request I did while writing this .

    它们之间存在行为差异,尤其是连字符运算符 %--% 如何工作 .

    文档很少,所以接下来是实证结论 .

    所有测试都假设:

    require 'erb'
    require 'erubis'
    

    When you can use -

    • ERB:您必须将 - 传递给 ERB.newtrim_mode 选项才能使用它 .

    • 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
    

    What -% does:

    • ERB:如果是换行符,则删除下一个字符 .

    • erubis:

    _359_在 <% %> (没有 = )中, - 没用,因为 <% %><% -%> 是相同的 . <% %> 如果只包含空格,则删除当前行,否则不执行任何操作 .

    • in <%= -%> (with = ):

    • 如果只包含空格,则删除整行

    • 否则,如果标签前面有非空格,并且只有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
    

    What %- does:

    • ERB:在标记之前和之前的换行符之后删除空格,但前提是只有空格 .

    • erubis:没用,因为 <%- %><% %> (没有 = )相同,并且这不能与 = 一起使用,这是 -% 唯一有用的情况 . 所以永远不要使用它 .

    例子:

    # 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

    两种效果的确切组合分开 .

  • 1

    <% %> 在那里执行代码但不打印结果,例如:
    我们可以将它用于erb文件中的if else .

    <% temp = 1 %>
    <% if temp == 1%>
      temp is 1
    <% else %>
      temp is not 1
    <%end%>
    

    将打印 temp is 1


    <%= %> 执行代码并打印输出,例如:
    我们可以打印rails变量的值 .

    <% temp = 1 %>
    <%= temp %>
    

    将打印 1


    <% -%> 它没有任何区别,因为它不打印任何东西, -%> 只对 <%= -%> 有意义,这将避免换行 .


    <%# %> 将注释掉在此内写的代码 .

  • 1
    <% %>
    

    在括号内执行ruby代码 .

    <%= %>
    

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

    <% -%>
    

    表达后避免换行 .

    <%# %>
    

    注释括号内的代码;没有发送到客户端(而不是HTML注释) .

    有关ERB的更多信息,请访问Ruby Doc .

相关问题