Using UTCTime with Hamlet

I use Yesod on my first site and I have a list of news:

NewsItem date UTCTime default=CURRENT_TIME title String content String author String 

which are retrieved in my handler:

 newsitems <- runDB $ selectList [] [Desc NewsItemDate] 

and is ultimately used in my template:

 $if null newsitems <p>No news. $else $forall Entity id entry <- newsitems <article> <h4>#{newsItemDate entry} <p>#{newsItemContent entry} 

But I get an error about data types:

 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)); .... } 

So, I suppose I would like to continue and add to my 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) 

Which compiles but gives me a runtime error in the browser:

 Internal Server Error PersistMarshalError "Expected UTCTime, received PersistText \"2012-08-30\"" 

So I'm not sure how to solve this, any ideas?

EDIT: Source code for the site in case of need or curiosity: https://github.com/iaefai/socrsite

+4
source share
2 answers

Without investigating the actual error, I think your approach is small. In the end, you will most likely want several ways to format UTCTime , because the type is stored there once, not just the date. By providing an instance of ToMarkup UTCTime , you fix it globally.

I would recommend writing functions renderAsDate :: UTCDate -> HTML , renderAsTime :: UTCDate -> HTML , etc. and use them in your template, for example. #{renderAsDate (newsItemDate entry)} .

But this will not solve the runtime error, which comes from the serialization level and probably does not depend on your templates.

+4
source

I'm sure you can just use the show in the village? This is at least what I did ...

 #{show $ newsItemDate entry} 

I used to come across this instance, and as this guy describes here, it looks something like this:

As part of this thrift philosophy, Haskell expressions doesn’t require type signing - although the experienced Haskeller provides them for clarity - therefore type errors in this strongly typed language are often cryptic to the uninitiated. For example, if you define a function f that adds two numbers and then calls them in two lines, the compiler will not complain about bad arguments, it will complain about a line that does not support the plus operator. And he will formulate this complaint in a very non-obvious way. [1] Under "1. Haskell Terse" ...

[1] http://fpcomplete.com/ten-things-you-should-know-about-haskell-syntax/

+1
source

Source: https://habr.com/ru/post/1432412/


All Articles