McBear Holden
McBear Holden

Reputation: 5901

Yesod: How to give UTCTime a toMarkup instance?

I have this Haskell Data in the models:

Vocabulary json
    word Text
    date UTCTime
    deriving Show

I get errors with the following code:

getHomeR :: Handler Html
getHomeR = do
    wordList <- runDB $ selectList [] [] :: HandlerT App IO [Entity Vocabulary]
    defaultLayout $ do
      setTitle "test"
      [whamlet|
              <ul>
                $forall Entity wordid worditem <- wordList
                  <li>#{vocabularyDate worditem} 
      |]

Error is:

 Could not deduce (blaze-markup-0.7.1.1:Text.Blaze.
ToMarkup
                          UTCTime)
        arising from a use of ‘toHtml’
      from the context: PersistEntity Vocabulary

I read from This post that I should declare UTCTime to be an instance of ToMarkup. How and in which file should I do this?

Upvotes: 0

Views: 179

Answers (1)

epsilonhalbe
epsilonhalbe

Reputation: 15959

I think you can do something like

import Data.Time.Format

getHomeR :: Handler Html
getHomeR = do
wordList <- runDB $ selectList [] [] :: HandlerT App IO [Entity Vocabulary]
defaultLayout $ do
  setTitle "test"
  [whamlet|
          <ul>
            $forall Entity wordid worditem <- wordList
              <li>#{dateFormat $ vocabularyDate worditem} 
  |]

dateFormat :: UTCTime -> String
dateFormat = formatTime defaultTimeLocale "%F"

I am not quite familiar with hamlet templates, so you might need to tweak this code a bit.

Upvotes: 1

Related Questions