我的 ERB 實驗遇到了一個奇怪的問題。這里我有什么代碼:
# cat ./services_lbs2.erb
<%= def renderVip(description, template)
puts "zxc dfg"
end
%>
# locally and remote
Printing: <%= renderVip('123','456') %>
這是我在 irb 中得到的:
irb(main):034:0> @template=File.read('./services_lbs2.erb')
=> "<%= def renderVip(description, template)\nputs \"zxc dfg\"\nend\n%>\n# locally and remotely monitored (all externals)\nPrinting: <%= renderVip('123','456') %>\n"
irb(main):035:0> zxc = ERB.new(@template,nil, "-")
=> #<ERB:0x00000000068d4d88 @safe_level=nil, @src="#coding:US-ASCII\n_erbout = String.new; _erbout.concat(( def renderVip(description, template)\nputs \"zxc dfg\"\nend\n).to_s); _erbout.concat \"\\n# locally and remotely monitored (all externals)\\nPrinting: \"\n\n; _erbout.concat(( renderVip('123','456') ).to_s); _erbout.concat \"\\n\"\n; _erbout.force_encoding(__ENCODING__)", @encoding=#<Encoding:US-ASCII>, @frozen_string=nil, @filename=nil, @lineno=0>
irb(main):036:0> zxc.result(binding)
zxc dfg
=> "renderVip\n# locally and remotely monitored (all externals)\nPrinting: \n"
我無法得到如下輸出:
# locally and remotely monitored (all externals)\nPrinting: zxc dfg\n
為什么會這樣以及如何解決?
謝謝!
uj5u.com熱心網友回復:
在您的情況下,回傳值puts函式是nil如此,該方法將回傳nil。
所以在執行之后nil被附加在body標簽內。為此,將其更改為
<%
def renderVip(description, template)
"zxc dfg"
end
%>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406648.html
標籤:
