我想做一些类似于我在下面尝试的事情。看来我需要更多地了解 Monad。有什么指点吗? 我确实使用了 yesod init 来开始,并使用 yesod add-handler 来创建处理程序。
在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)
最佳答案
此错误消息的含义是“我知道 hello
变量是某种 IsString 实例,但我不知道是哪一个。”换句话说,hello
的类型可能是 String
、Text
、Html
或其他类型,但是编译器根据您提供的信息无法判断。最简单的解决方法是添加显式类型签名,例如:
let hello :: String
hello = "Hello World"
这是使用OverloadedStrings
时出现的一个常见问题。
关于haskell - 如何在 Yesod 处理程序中将其他变量传递给 defaultLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182022/