1

我正在尝试在我的 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...

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

4

1 回答 1

1

问题是:

let test = fmap extraStatic getExtra

getExtra是一个Handler动作,所以你想要的是:

test <- fmap extraStatic getExtra
于 2013-10-14T18:41:06.260 回答