为什么许多人使用“ - %>”而不是“%>”。在Rails? [英] Why many people use "-%>" instead of "%>" in Rails?
问题描述
对不起,这个问题,我认为它更多的offtopic,但我找不到任何东西在谷歌!
Sorry for this question, i think its more offtopic, but i couldn't find anything on google!
我看到现在多次,很多人使用 - %>
,而不是仅仅%>
。有什么意义?
I saw now multiple times that a lot of people use -%>
instead of just %>
. Whats the sense?
示例:
<% @images.each_slice(6) do |slice| -%>
<div class="gallery">
<% slice.each do |image| -%>
<%= image_tag(image.url, :alt => image.alt) %>
<% end -%>
</div>
<% end -%>
Source: Rails each loop insert tag every 6 items?
在这里,他还使用了<$
Here he has also used -%>
for all blocks.
推荐答案
我想添加一些我知道有关ERB的资源:
I would like to add some resources that I know about ERB :
-
Rails扩展
ERB
,以便您可以通过在Rails模板中的标签中添加结尾连字符来取消换行符:
<ul>
<% for @item in @items -%>
<li><%= @item %></li>
<% end -%>
</ul>
注释标记使用 hash
符号:
<%# This is just a comment %>
具有 等于号的标签
表示封闭代码是一个表达式,并且渲染器应该在渲染模板时用代码结果(作为字符串)替换代码元素。使用表达式将一行代码嵌入到模板中,或显示变量的内容:
A tag with an equals sign
indicates that enclosed code is an expression, and that the renderer should substitute the code element with the result of the code (as a string) when it renders the template. Use an expression to embed a line of code into the template, or to display the contents of a variable:
Hello, <%= @name %>.
Today is <%= Time.now.strftime('%A') %>.
使用一个 等号
字符串将被编码。要避免编码,您可以使用两个 等号
(或 raw ):
Hello, <%== @unencodedOutput %>
不含 c>
符号表示附带的代码是 scriptlet 。每个scriptlet被捕获和执行,然后代码的最终结果被注入到 scriptlet 的输出。
Tags without the equals
sign denote that the enclosed code is a scriptlet. Each scriptlet is caught and executed, and the final result of the code is then injected in to the output at the point of the scriptlet.
<ul>
<% for @item in @shopping_list %>
<li><%= @item %></li>
<% end %>
</ul>
Scriptlet最常用于将循环或条件逻辑嵌入到模板中:
Scriptlets are most commonly used for embedding loops or conditional logic into templates:
阅读 ERB模板介绍 了解更多。
Read An Introduction to ERB Templating to know more.
这篇关于为什么许多人使用“ - %>”而不是“%>”。在Rails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!