Używanie UTCTime z Hamletem
Używam Yesod na mojej pierwszej stronie i mam listę wiadomości:
NewsItem
date UTCTime default=CURRENT_TIME
title String
content String
author String
które są pobierane w moim podręczniku:
newsitems <- runDB $ selectList [] [Desc NewsItemDate]
i ostatecznie użyty w moim szablonie:
$if null newsitems
<p>No news.
$else
$forall Entity id entry <- newsitems
<article>
<h4>#{newsItemDate entry}
<p>#{newsItemContent entry}
Ale pojawia się błąd dotyczący typów danych:
Handler/Home.hs:20:11:
No instance for (Text.Blaze.ToMarkup
time-1.4:Data.Time.Clock.UTC.UTCTime)
arising from a use of `toHtml'
Possible fix:
add an instance declaration for
(Text.Blaze.ToMarkup time-1.4:Data.Time.Clock.UTC.UTCTime)
In the first argument of `toWidget', namely
`toHtml (newsItemDate entry_a6ev)'
In a stmt of a 'do' block:
toWidget (toHtml (newsItemDate entry_a6ev))
In the expression:
do { toWidget
((Text.Blaze.Internal.preEscapedText . Data.Text.pack)
"<article><h4>");
toWidget (toHtml (newsItemDate entry_a6ev));
toWidget
((Text.Blaze.Internal.preEscapedText . Data.Text.pack)
"</h4>\
\<p>");
toWidget (toHtml (newsItemContent entry_a6ev));
.... }
Więc myślę, że pójdę dalej i dodam do mojego pliku Import.hs:
import Data.Time (UTCTime)
import Data.Time.Format (formatTime)
import Text.Blaze (ToMarkup, toMarkup)
import Text.Blaze.Internal (string)
import System.Locale (defaultTimeLocale)
-- format date as 26 July 2012
instance ToMarkup UTCTime where
toMarkup a = string (formatTime defaultTimeLocale "%e %B %Y" a)
Który się kompiluje, ale daje mi błąd w czasie wykonywania w przeglądarce:
Internal Server Error
PersistMarshalError "Expected UTCTime, received PersistText \"2012-08-30\""
Więc nie jestem pewien, jak to rozwiązać, jakieś pomysły?
EDYCJA: Kod źródłowy na stronie w razie potrzeby lub ciekawy:https://github.com/iaefai/socrsite