2011-03-24 33 views
1

我有一個助手,需要一個塊:如何直接輸出在ERB中使用塊的幫助程序的結果?

def container(&block) 
    render(:partial => 'layouts/container', :locals => {:content => capture(&block)}) 
end 

當我嘗試在ERB使用它一個<%= ... %>標籤內:

<%= container do %> 
    Test 
<% end %> 

我得到一個compile error

compile error 
test2.html.erb:1: syntax error, unexpected ')' 
      old_output_buffer = output_buffer;;@output_buffer = ''; __in_erb_template=true ; @output_buffer.concat((container do).to_s); @output_buffer.concat "\r\n" 
                                    ^
test2.html.erb:4: syntax error, unexpected kENSURE, expecting ')' 
test2.html.erb:6: syntax error, unexpected kEND, expecting ')' 

但是,如果我捕獲幫助程序的輸出並輸出:

<% output = container do %> 
    Test 
<% end %> 
<%= output %> 

它工作正常,但很醜。

有沒有辦法做我想做的事? (注意:通常我使用HAML,但我試圖讓我的幫手爲仍然在ERB工作的團隊中的其他人員工作;切換到HAML不是解決方案)。

回答

0

,而不是採取信貸別人的工作,我就點你here.

+0

並沒有真正解決我的問題。我不想改變幫手的工作方式,因爲我希望它繼續在HAML中工作。另外,該項目使用Rails,所以我不直接打電話給ERB。 – 2011-03-24 17:13:30

相關問題