2013-11-24 43 views
1

我想要做類似於我在下面嘗試的內容。似乎我需要更多地瞭解Monads。任何指針? 我確實使用yesod init開始,yesod add-handler創建處理程序。如何在Yesod處理程序中將其他變量傳遞給defaultLayout?

Handler/Hello.hs

getHelloR :: Handler Html 
getHelloR = do 
    let hello = "Hello World" 
    defaultLayout $ do 
     $(widgetFile "hello") 

templates/hello.hamlet

<h1>Test 
<p>#{hello} 

錯誤運行cabal-dev install && yesod --dev devel 時,我得到的是:

Handler/Hello.hs:9:11: 
    Ambiguous type variable `a0' in the constraints: 
     (Data.String.IsString a0) 
     arising from a use of `hello' at Handler/Hello.hs:9:11-28 
     (blaze-markup-0.5.1.5:Text.Blaze.ToMarkup a0) 
     arising from a use of `toHtml' at Handler/Hello.hs:9:11-28 
    Probable fix: add a type signature that fixes these type variable(s) 
    In the first argument of `toHtml', namely `hello' 
    In the first argument of `asWidgetT . toWidget', namely 
     `toHtml hello' 
    In a stmt of a 'do' block: (asWidgetT . toWidget) (toHtml hello) 
+0

你得到什麼錯誤?你是否使用「yesod init」初始化?如果你這樣做,它會創建一個應該能夠工作的框架(如果不向我們顯示錯誤),然後你可以將它轉換爲你的代碼(它將非常接近你已經寫的東西,除了.hamlet文件將會更充實,但你可以刪除它的一切。 – jamshidh

+0

@jamshidh剛剛更新了現在的問題。謝謝 – Mattias

回答

3

什麼此錯誤消息意味着「我知道hello變量是某種IsString實例,但我不知道哪一個。「換句話說,hello將是StringText,Html或其他類型的類型,但編譯器根據您提供的信息無法弄清楚。最簡單的是解決,這是增加一個顯式簽名,例如:

let hello :: String 
    hello = "Hello World" 

這是使用OverloadedStrings時出現一個頗爲普遍的問題。

相關問題