Does it have something to do with output?
它与输出有关吗?
So, <%= ...code... %>
is used for outputting after code is executed, and <% ...code... %>
is only used for executing the code?
因此,<%= ... code ...%>用于在执行代码后输出,<%... code ...%>仅用于执行代码?
3 个解决方案
#1
10
This is ERB templating markup (one of many templating languages supported by Rails). This markup:
这是ERB模板标记(Rails支持的许多模板语言之一)。这个标记:
<% ... %>
is used to evaluate a Ruby expression. Nothing is done with the result of that expression, however. By contrast, the markup:
用于评估Ruby表达式。但是,该表达式的结果没有做任何事情。相比之下,加价:
<%= ... %>
does the same thing (runs whatever Ruby code is inside there) but it calls to_s
on the result and replaces the markup with the resulting string.
做同样的事情(运行里面的任何Ruby代码)但它在结果上调用to_s并用结果字符串替换标记。
In short:
简而言之:
<% just run code %>
<%= run code and output the result %>
For example:
例如:
<% unless @items.empty? %>
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>
<% end %>
In contrast, here's some Haml markup equivalent to the above:
相比之下,这里有一些等同于上面的Haml标记:
- unless @items.empty?
%ul
- @items.each do |item|
%li= item.name
#2
4
Both execute the Ruby code contained within them. However, the different is in what they do with the returned value of the expression. <% ... %>
will do nothing with the value. <%= ... %>
will output the return value to whatever document it is executed in (typically a .erb
or .rhtml
document).
两者都执行其中包含的Ruby代码。但是,不同之处在于它们对表达式返回值的处理方式。 <%...%>对值无效。 <%= ...%>会将返回值输出到执行的任何文档(通常是.erb或.rhtml文档)。
Something to note, <%= ... %>
will automatically escape any HTML contained in text. If you want to include any conditional HTML statements, do it outside the <% ... %>
.
需要注意的是,<%= ...%>将自动转义文本中包含的任何HTML。如果要包含任何条件HTML语句,请在<%...%>之外执行。
<%= "<br />" if need_line_break %> <!-- Won't work -->
<% if need_line_break %>
<br />
<% end %> <!-- Will work -->
#3
3
<%=
is used to output the value of a single expression, e.g.
<%=用于输出单个表达式的值,例如
<%= object.attribute %>
<%= link_to 'Link Title', link_path %>
while <%
is used to execute arbitrary Ruby code.
而<%用于执行任意Ruby代码。
#1
10
This is ERB templating markup (one of many templating languages supported by Rails). This markup:
这是ERB模板标记(Rails支持的许多模板语言之一)。这个标记:
<% ... %>
is used to evaluate a Ruby expression. Nothing is done with the result of that expression, however. By contrast, the markup:
用于评估Ruby表达式。但是,该表达式的结果没有做任何事情。相比之下,加价:
<%= ... %>
does the same thing (runs whatever Ruby code is inside there) but it calls to_s
on the result and replaces the markup with the resulting string.
做同样的事情(运行里面的任何Ruby代码)但它在结果上调用to_s并用结果字符串替换标记。
In short:
简而言之:
<% just run code %>
<%= run code and output the result %>
For example:
例如:
<% unless @items.empty? %>
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>
<% end %>
In contrast, here's some Haml markup equivalent to the above:
相比之下,这里有一些等同于上面的Haml标记:
- unless @items.empty?
%ul
- @items.each do |item|
%li= item.name
#2
4
Both execute the Ruby code contained within them. However, the different is in what they do with the returned value of the expression. <% ... %>
will do nothing with the value. <%= ... %>
will output the return value to whatever document it is executed in (typically a .erb
or .rhtml
document).
两者都执行其中包含的Ruby代码。但是,不同之处在于它们对表达式返回值的处理方式。 <%...%>对值无效。 <%= ...%>会将返回值输出到执行的任何文档(通常是.erb或.rhtml文档)。
Something to note, <%= ... %>
will automatically escape any HTML contained in text. If you want to include any conditional HTML statements, do it outside the <% ... %>
.
需要注意的是,<%= ...%>将自动转义文本中包含的任何HTML。如果要包含任何条件HTML语句,请在<%...%>之外执行。
<%= "<br />" if need_line_break %> <!-- Won't work -->
<% if need_line_break %>
<br />
<% end %> <!-- Will work -->
#3
3
<%=
is used to output the value of a single expression, e.g.
<%=用于输出单个表达式的值,例如
<%= object.attribute %>
<%= link_to 'Link Title', link_path %>
while <%
is used to execute arbitrary Ruby code.
而<%用于执行任意Ruby代码。