【问题标题】:Yesod custom settings.yml field type mismatchYesod 自定义 settings.yml 字段类型不匹配
【发布时间】:2013-10-14 15:32:51
【问题描述】:

我正在尝试在我的 settings.yml 文件中为我的静态文件的位置创建一个新字段(这样我就可以从开发中的本地子目录更改为生产中的 CDN),但我似乎无法获得基本的“你好世界”。这是我的 Settings.hs:

data Extra = Extra
    { extraCopyright :: Text
    , extraAnalytics :: Maybe Text -- ^ Google Analytics
    , extraStatic :: Text
    } deriving Show

parseExtra :: DefaultEnv -> Object -> Parser Extra
parseExtra _ o = Extra
    <$> o .:  "copyright"
    <*> o .:? "analytics"
    <*> o .:  "static"

这里是 settings.yml 的相关部分:

Default: &defaults
  host: "*4" # any IPv4 host
  port: 3000
  approot: "http://localhost:3000"
  copyright: Insert copyright statement here
  #analytics: UA-YOURCODE
  static: "/static"

最后是引发错误的部分。我真的不知道我应该如何在请求中包含它的值,所以我只是将代码放入其中以在每个请求上执行:

getHomeR :: Handler Html
getHomeR = do
    (formWidget, formEnctype) <- generateFormPost sampleForm
    let submission = Nothing :: Maybe (FileInfo, Text)
        handlerName = "getHomeR" :: Text
        test = fmap extraStatic getExtra
    defaultLayout $ do
        aDomId <- newIdent
        setTitle "Welcome To Yesod!"
        $(widgetFile "homepage")

我在 postHomeR 上也添加了相同的行(test = fmap extraStatic getExtra)。我的村庄:

<p>#{test}

最后,屏幕上抛出的错误:

Handler/Home.hs:37:11:
    No instance for (blaze-markup-0.5.1.5:Text.Blaze.ToMarkup
                       (HandlerT App IO Text))
      arising from a use of `toHtml'
    Possible fix:
      add an instance declaration for
      (blaze-markup-0.5.1.5:Text.Blaze.ToMarkup (HandlerT App IO Text))
    In the first argument of `Yesod.Core.Widget.asWidgetT
                              . toWidget', namely
       `toHtml test'
    In a stmt of a 'do' block:
      (Yesod.Core.Widget.asWidgetT . toWidget) (toHtml test)
    In a stmt of a 'do' block:
      do { (Yesod.Core.Widget.asWidgetT . toWidget)
             ((blaze-markup-0.5.1.5:Text.Blaze.Internal.preEscapedText
               . Data.Text.pack)
                "<h1>");
           ((Control.Monad.liftM (toHtml .) getMessageRender)
            >>=
               (\ urender_aaYh
                 -> (Yesod.Core.Widget.asWidgetT . toWidget)
                       (urender_aaYh MsgHello)));
          (Yesod.Core.Widget.asWidgetT . toWidget)
             ((blaze-markup-0.5.1.5:Text.Blaze.Internal.preEscapedText
               . Data.Text.pack)
                "</h1>\
                \<ol><li>Now that you have a working project you should use the <a href=\"http://www.yesodweb.com/book/\">Yesod book</a> to learn more. You can also use this scaffolded site to explore some basic concepts.</li><li> This page was generated by the ");
           (Yesod.Core.Widget.asWidgetT . toWidget) (toHtml handlerName);
           .... }
Build failure, pausing...

有什么办法可以施放吗?有什么方法可以让变量在应用程序启动期间被分配和加载,但可以在整个请求中作为常量值访问?任何帮助将不胜感激!

【问题讨论】:

    标签: haskell casting yesod blaze-html


    【解决方案1】:

    问题是:

    let test = fmap extraStatic getExtra
    

    getExtraHandler 动作,所以你想要的是:

    test <- fmap extraStatic getExtra
    

    【讨论】:

    • 谢谢。我希望有一天能像你一样胜任。值是否被缓存?这样每个请求都不会被阻塞?
    • 另外,我如何让变量在布局级别而不是小部件级别可见?
    • 我不会为此担心缓存。它正在执行 Handler 操作,但它本质上只是为 Reader monad 调用 ask。我不确定在布局级别使变量可见是什么意思。
    • 啊,好吧,这真的很酷。如果你不能说,我是一个超级菜鸟哈哈。但我解决了。再次感谢您的帮助!
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多