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

可否请您描述一下ERB文件中使用的如下字符的用法: html

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

每一个有什么用? git


#1楼

Rails默认使用stdlib的ERB ,而是使用erubis 。 资料来源: 该开发人员的评论ActionView的gemspec 接受了我在编写此 文档时所作的 合并请求github

它们之间行为的差别,特别是关于如何连字符运营%--%的工做。 c#

文档稀缺, Ruby的ERB格式在哪里“正式”定义? 所以,如下是经验结论。 浏览器

全部测试都假定: ruby

require 'erb'
require 'erubis'

什么时候能够使用- less

  • ERB:你必须经过-trim_mode的选项ERB.new使用它。
  • 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

-%是: 测试

  • ERB:若是下一个字符是换行符,则将其删除。 ui

  • erubis:

    • <% %> (无= )中, -没用,由于<% %><% -%>相同。 <% %>若是仅包含空格,则删除当前行,不然不执行任何操做。

    • <%= -%> (带有= ):

      • 若是只包含空格,则删除整行
      • 不然,若是标记前没有空格,然后有空白,则删除后面的空白
      • 不然,标记后会有一个非空格:什么都不作

例子:

# 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

%-做用:

  • 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

%--%一块儿作什么

两种效果的确切组合分别存在。


#2楼

因为其晦涩难懂,我添加了<%%文字标签订界符做为对此的答案。 这将告诉erb不要解释标记的<%部分,这对于js应用程序(例如显示chart.js工具提示等)是必需的。

更新(修复断开的连接)

如今能够在如下位置找到有关ERB的全部信息: https : //puppet.com/docs/puppet/5.3/lang_template_erb.html#tags


#3楼

这些是在轨道上的红宝石中使用的

<%%>:-

<%%>标记用于执行不返回任何条件(例如条件,循环或块)的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" %>

#4楼

<% %>在其中执行代码,但不打印结果,例如:
咱们能够将其用于erb文件中。

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

将打印temp is 1


<%= %>执行代码并打印输出,例如:
咱们能够打印rails变量的值。

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

将列印1


<% -%>没什么区别,由于它不打印任何内容, -%>仅对<%= -%>有意义,这样能够避免换行。


<%# %>将注释掉其中编写的代码。


#5楼

  • <% %> :执行红宝石代码
  • <%= %> :打印到Erb文件中。 或浏览器
  • <% -%> :避免在表达式后换行。
  • <%# %> :ERB评论
相关文章
相关标签/搜索