【问题标题】:How do I pass additional variables to defaultLayout in a Yesod handler?如何在 Yesod 处理程序中将其他变量传递给 defaultLayout?
【发布时间】:2013-12-09 12:38:40
【问题描述】:

我想做一些类似于我在下面尝试的事情。似乎我需要了解更多关于 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 &amp;&amp; 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 刚刚更新了这个问题。谢谢

标签: haskell yesod


【解决方案1】:

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

let hello :: String
    hello = "Hello World"

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

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 2021-09-07
    • 2018-08-03
    • 1970-01-01
    • 2020-09-04
    • 2013-06-03
    • 2021-05-19
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多