2012-02-07 17 views
9

在導軌3.0 HAML(3.1.4)我有content_for VS在泛音收率

  1. 一些模板狀局部,像_template.html.haml:

    .panel.top 
        = yield :panel_top 
    
    .content 
        = yield 
    
  2. 另一個部分這將顯示使用prev模板(所有這些東西使用AJAX渲染,但這並不重要)

    - content_for :panel_top do 
    .title.left 
        = title 
    
    content text 
    

,這工作就像一個魅力在Rails的3.0

但是,升級後3.2失敗! Yiels只是得到「內容文字」,所以我有「內容文字」兩次,在所有

沒有標題只改變= yield :panel_top= content_for :panel_top作品3.2

我不知道,這個解決方案是確定的,如果它是穩定的或推薦的,我找不到任何關於yield處理中的變化的註釋,也不能在Rails 3.1發行說明中找到任何說明,也不能在3.2中找到。

你能幫助什麼是最好的方式組織yield ing在部分?

回答

10

從Rails的3.0到Rails 3.2 content_for是真的變了:

3.0

def content_for(name, content = nil, &block) 
    content = capture(&block) if block_given? 
    @_content_for[name] << content if content 
    @_content_for[name] unless content 
end 

3.2

def content_for(name, content = nil, &block) 
    if content || block_given? 
    content = capture(&block) if block_given? 
    @view_flow.append(name, content) if content 
    nil 
    else 
    @view_flow.get(name) 
    end 
end 

這告訴我們,從3.2 content_for作品用於顯示/插入內容,不僅存儲它或命名的部分。

此外,如果您嘗試調試yield邏輯,您會認爲它在content_for正確初始化之前產生。

所以,留下片段緩存我們的討論中我可以斷定,content_for是任意位置插入一個名爲段除頂層佈局preferrable方式。在幫手和其他情況下,yield應該呈現錯誤的結果。