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

时间:2017-02-26 18:04:30

标签: haskell yesod

我在模型中有这个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的一个实例。我应该如何以及在哪个文件中执行此操作?

1 个答案:

答案 0 :(得分:1)

我认为你可以做点什么

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模板不是很熟悉,所以你可能需要调整一下这段代码。