Yesod:如何给 UTCTime 一个 toMarkup 实例?

Yesod: How to give UTCTime a toMarkup instance?

我有这个 Haskell 模型中的数据:

Vocabulary json
    word Text
    date UTCTime
    deriving Show

我收到以下代码的错误:

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} 
      |]

错误是:

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

我从 This post 了解到我应该将 UTCTime 声明为 ToMarkup 的一个实例。我应该如何以及在哪个文件中执行此操作?

我想你可以这样做

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"

我不太熟悉 hamlet 模板,所以您可能需要稍微调整一下这段代码。