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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7996695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:17:06  来源:igfitidea点击:

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

ruby-on-railsrubytemplateserb

提问by simo

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 ?

每一种的用途是什么?

回答by auralbee

<% %>

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 Docfor more infos about ERB.

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

回答by yalestar

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

<% %>并且<%- and -%>适用于任何 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

回答by Neha Nakrani

  • <% %>: Executes the ruby code
  • <%= %>: Prints into Erb file. Or browser
  • <% -%>: Avoids line break after expression.
  • <%# %>: ERB comment
  • <% %>: 执行 ruby​​ 代码
  • <%= %>: 打印到 Erb 文件中。或者浏览器
  • <% -%>: 避免在表达式后换行。
  • <%# %>: ERB 评论

回答by bkunzi01

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.

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

Update (Fixed broken link)

更新(修复断开的链接)

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

现在可以在此处找到有关 ERB 的所有信息:https: //puppet.com/docs/puppet/5.3/lang_template_erb.html#tags

回答by HeadAndTail

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" %>

回答by Aastha Kesarwani

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

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

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

Will print temp is 1

会打印 temp is 1



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

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

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

Will print 1

会打印 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.

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