我希望看到"test“一次出现在输出中,而"hello”则出现一次。
但我对这样一个事实感到困惑:如果我这样做, "test“一词将显示两次。
<div>
<h3>test</h3>
</div>
<% def helo %>
<% "hello" %>
<% end %>
<%= helo %>我想有一个简单的解释,这与一些怪癖的erb?
发布于 2009-09-15 04:41:03
我试过:
require 'erb'
template = %q{
<div>
<h3>test</h3>
</div>
<% def helo %>
<% "hello" %>
<% end %>
<%= helo %>
}
t = ERB.new(template)
puts t.result
#(erb):6:in `helo': undefined local variable or method `_erbout' for main:Object (NameError) from (erb):10因此,你提到的似乎是正确的,但在所有的方法中,你都可以很容易地欺骗它:
require 'erb'
template = %q{
<div>
<h3>test</h3>
</div>
<% def helo
"hello"
end %>
<%= helo %>
}
message = ERB.new(template)
puts message.result对我来说很管用。
https://stackoverflow.com/questions/1425090
复制相似问题