我正在尝试在我的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...
我有什么方法可以施展它吗?有没有什么办法可以让我在app启动时分配和加载变量,但是作为常量值可以在整个请求中访问?任何帮助将不胜感激!!
答案 0 :(得分:1)
问题是:
let test = fmap extraStatic getExtra
getExtra
是Handler
行为,所以您想要的是:
test <- fmap extraStatic getExtra