如何在Yesod处理程序中将其他变量传递给defaultLayout?

时间:2013-11-24 22:38:37

标签: haskell yesod

我想做类似于我在下面尝试的事情。好像我需要了解更多关于Monads的信息。有什么指针吗? 我确实使用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)

1 个答案:

答案 0 :(得分:3)

此错误消息的含义是“我知道hello变量是某种IsString实例,但我不知道哪一个。”换句话说,hello类型为StringTextHtml或其他类型,但编译器无法根据您提供的信息找出它重新提供。最简单的解决方法是添加一个显式类型签名,例如:

let hello :: String
    hello = "Hello World"

这是使用OverloadedStrings时出现的一个常见问题。