ruby erb中的(不等于)意味着什么?
我发现这个“没有替换回输出执行”,但也许我的英语不太好,我真的不明白这意味着什么。 任何人都可以帮忙吗?
<%%>
将执行Ruby代码而不会影响正在呈现的html页面。 输出将被丢弃。
<%=%>
将执行Ruby代码并插入该代码的输出来代替<%=%>
例…
<% puts "almost" %> nothing to see here
会呈现为
nothing to see here
然而
<%= puts "almost" %> nothing to see here
会呈现为
almost nothing to see here
有时你必须(或者你想)执行一些ruby语句但不是出于输出目的。
如下:
<% if @user.nil? %> Hi, welcome! <% else %> Hi, <%= @user.name %>! <% end %>
当然这仅仅是一个用例,但有时你确实需要<% %>
:D
<% %>
代码(不等于)执行“没有替换回输出”意味着你想要执行没有任何输出的代码,比如循环,最好的部分是,它可以与非ruby代码一起使用。
<% 3.times do %> Hello world
<%end%> This will give: Hello world
Hello world
Hello world