1

Since IO can not be used inside Yesod Template, how can I display a simple current time on a page?

In my .hamlet file, something like:

<h2>
 #{show $ getCurrentTime } 

getCurrentTime :: IO UTCTime
Community
  • 1
  • 1
McBear Holden
  • 5,741
  • 7
  • 33
  • 55

1 Answers1

4

In other words, you need to run the IO action outside of the template.

That outside means the template's handler. So I would write like this.

-- Home.hs
getHomeR = do
  time <- liftIO getCurrentTime
  defaultLayout $(widgetFile "homepage")

-- homepage.hamlet
<h2>#{show time}
jeiea
  • 1,965
  • 14
  • 24
  • This `getCurrentTime` return time in UTC or in local timezone? What timezone will the user see in the browser? – Fábio Roberto Teodoro Jul 28 '17 at 19:49
  • 1
    @frt You can test it with ghci, `import Data.Time` `getCurrentTime`. It will show us `2017-07-28 23:32:26.2083905 UTC` `it :: UTCTime`. – jeiea Jul 28 '17 at 23:35