2011-10-19 30 views
11

我目前正在通過遵循tutorial on the Yesod Wiki來實驗Yesod。鏈接到哈姆雷特的一個靜態文件

首先,我創建了使用yesod init一個耶索德應用程序,並創建了渲染稱作homepage一個widget文件根處理程序:

getRootR = do 
mu <- maybeAuth 
defaultLayout $ do 
    h2id <- lift newIdent 
    setTitle "Home" 
    addWidget $(widgetFile "homepage") 

我在靜態目錄呼叫static/img/logo.png

觸摸後的圖像文件Settings/staticFiles.hs,我成功地管理通過

<img [email protected]{StaticR img_logo_png} 
default-layout.hamlet該文件鏈接

現在的問題是,我想使用完全相同的代碼行將此靜態文件包含在我的homepage小部件中。在編譯時會出現以下錯誤:

Handler/Root.hs:19:21: 
    Not in scope: `img_logo_png' 
    In the result of the splice: 
     $(widgetFile "homepage") 
    To see what the splice expanded to, use -ddump-splices 
    In the first argument of `addWidget', namely 
     `$(widgetFile "homepage")' 
    In the expression: addWidget ($(widgetFile "homepage")) 

所以我的問題是:我怎麼與widgetFile定義的小部件鏈接的靜態資源,爲什麼它會表現不同的默認佈局模板?

回答

6

您需要進口增加處理器/ Root.hs:

import Settings.StaticFiles 

如果一個小村莊文件需要它,然後取其handler.hs文件調用該文件的小村莊將需要先導入。之所以default-layout.hamlet不需要任何改變,是因爲它在某個地方被調用,我相信Application.hs的導入幾乎包括所有的東西,包括靜態的東西。