如何使用 Yesod 模板语言显示当前时间?
How to show current time with Yesod Template languages?
从IO can not be used inside Yesod Template开始,如何在页面上显示一个简单的当前时间?
在我的 .hamlet 文件中,类似于:
<h2>
#{show $ getCurrentTime }
getCurrentTime :: IO UTCTime
In other words, you need to run the IO action outside of the template.
那个 outside 表示模板的 handler。所以我会这样写。
-- Home.hs
getHomeR = do
time <- liftIO getCurrentTime
defaultLayout $(widgetFile "homepage")
-- homepage.hamlet
<h2>#{show time}
从IO can not be used inside Yesod Template开始,如何在页面上显示一个简单的当前时间?
在我的 .hamlet 文件中,类似于:
<h2>
#{show $ getCurrentTime }
getCurrentTime :: IO UTCTime
In other words, you need to run the IO action outside of the template.
那个 outside 表示模板的 handler。所以我会这样写。
-- Home.hs
getHomeR = do
time <- liftIO getCurrentTime
defaultLayout $(widgetFile "homepage")
-- homepage.hamlet
<h2>#{show time}