我希望看到单词“test”出现在输出中一次,单词“hello”出现一次。
但令我困惑的是,如果我这样做,“测试”这个词会显示两次。
<div>
<h3>test</h3>
</div>
<% def helo %>
<% "hello" %>
<% end %>
<%= helo %>
我认为有一个与 erb 的一些怪癖有关的简单解释?
我希望看到单词“test”出现在输出中一次,单词“hello”出现一次。
但令我困惑的是,如果我这样做,“测试”这个词会显示两次。
<div>
<h3>test</h3>
</div>
<% def helo %>
<% "hello" %>
<% end %>
<%= helo %>
我认为有一个与 erb 的一些怪癖有关的简单解释?
我尝试过这个:
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
它对我有用。