<%、<%=、<%# 和 -%> 之间有什么区别?在Rails 中的ERB 中?

发布于 2024-12-13 06:49:31 字数 137 浏览 2 评论 0原文

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

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

每个字符的用法是什么?

Can some one please describe the usage of the following characters which is used in ERB file:

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

what's the usage of each one ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫瑾少女 2024-12-20 06:49:31
<% %>

执行括号内的 ruby​​ 代码。

<%= %>

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

<%== %>

相当于 <%= raw %>。将一些内容逐字打印(即不转义)到 erb 文件中。 (摘自 Ruby on Rails 指南。)

<% -%>

避免表达式后换行。

<%# %>

注释掉括号内的代码;不发送给客户端(与 HTML 注释相反)。

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

<% %>

Executes the ruby code within the brackets.

<%= %>

Prints something into erb file.

<%== %>

Equivalent to <%= raw %>. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)

<% -%>

Avoids line break after expression.

<%# %>

Comments out code within brackets; not sent to client (as opposed to HTML comments).

Visit Ruby Doc for more infos about ERB.

伪装你 2024-12-20 06:49:31

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

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

<%# %> 是 ERB 注释

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

<% %> and <%- and -%> are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.

<%= %> is for outputting the results of Ruby code

<%# %> is an ERB comment

Here's a good guide:
http://api.rubyonrails.org/classes/ActionView/Base.html

逆夏时光 2024-12-20 06:49:31

Rails 使用stdlib ERB 默认情况下,它使用 erubis。资料来源:此开发者的评论ActionView 的 gemspec, 接受了我在编写本文时所做的合并请求

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

文档很少,Ruby 的 ERB 格式“官方”定义在哪里?以下是经验结论。

所有测试假设:

require 'erb'
require 'erubis'

当您可以使用 -

  • 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:如果是换行符,则删除下一个字符。

  • 埃鲁比斯:

    • <% %> 中(没有 =),- 没有用,因为 <% % ><% -%> 相同。 <% %> 如果当前行仅包含空格,则删除当前行,否则不执行任何操作。

    • <%= -%> 中(使用 =):

      • 如果仅包含空格,则删除整行
      • 否则,如果标签前面没有空格,并且后面只有空格,则删除后面的空格
      • 否则,标签后有一个非空格:不执行任何操作

操作 示例:

# 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

%--% 一起做什么

两种效果的精确组合。

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.

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.

All tests suppose:

require 'erb'
require 'erubis'

When you can use -

  • ERB: you must pass - to trim_mode option of ERB.new to use it.
  • erubis: enabled by default.

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:

    • in <% %> (without =), - is useless because <% %> and <% -%> are the same. <% %> removes the current line if it only contains whitespaces, and does nothing otherwise.

    • in <%= -%> (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.

  • 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.

淡淡绿茶香 2024-12-20 06:49:31
  • <% %> :执行 ruby​​ 代码
  • <%= %> :打印到 Erb 文件中。或浏览器
  • <% -%> :避免表达式后换行。
  • <%# %>:ERB 评论
  • <% %> : Executes the ruby code
  • <%= %> : Prints into Erb file. Or browser
  • <% -%> : Avoids line break after expression.
  • <%# %> : ERB comment
亽野灬性zι浪 2024-12-20 06:49:31

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

更新(修复了损坏的链接)

一切关于 ERB 现在可以在这里找到:
https://puppet.com/docs/puppet/5.3/lang_template_erb.html#标签

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.

Update (Fixed broken link)

Everything about ERB can now be found here:
https://puppet.com/docs/puppet/5.3/lang_template_erb.html#tags

输什么也不输骨气 2024-12-20 06:49:31

这些在 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" %>

These are use in ruby on rails :-

<% %> :-

The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-

<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

<%# %>:-

comment out the code

<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
腹黑女流氓 2024-12-20 06:49:31

<% %> 执行其中的代码但不打印结果,例如:
我们可以在 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


<% -%> 这没有什么区别,因为它不打印任何内容, -%> 仅对以下内容有意义<%= -%>,这将避免换行。


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

<% %> executes the code in there but does not print the result, for eg:
We can use it for if else in an erb file.

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

Will print temp is 1


<%= %> executes the code and also prints the output, for eg:
We can print the value of a rails variable.

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

Will print 1


<% -%> It makes no difference as it does not print anything, -%> only makes sense with <%= -%>, this will avoid a new line.


<%# %> will comment out the code written within this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文