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