我在我的第一个网站上使用Yesod,我有一个新闻项目列表:
NewsItem
date UTCTime default=CURRENT_TIME
title String
content String
author String它们在我的处理程序中检索:
newsitems <- runDB $ selectList [] [Desc NewsItemDate]并最终在我的模板中使用:
$if null newsitems
<p>No news.
$else
$forall Entity id entry <- newsitems
<article>
<h4>#{newsItemDate entry}
<p>#{newsItemContent entry}但是我得到了一个关于数据类型的错误:
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));
.... }所以我想我应该继续添加到我的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)它进行了编译,但在运行时在浏览器中显示了一个错误:
Internal Server Error
PersistMarshalError "Expected UTCTime, received PersistText \"2012-08-30\""所以我不确定如何解决这个问题,有什么想法吗?
编辑:网站的源代码,以备需要或好奇时使用:https://github.com/iaefai/socrsite
发布于 2012-09-05 15:18:56
在不调查实际错误的情况下,我认为您的方法不是很好。您最终很可能需要几种格式化UTCTime的方法,毕竟,该类型是用来存储时间的,而不仅仅是日期。通过提供一个ToMarkup UTCTime实例,您可以在全局范围内修复此问题。
我建议编写函数renderAsDate :: UTCDate -> HTML、renderAsTime :: UTCDate -> HTML等,并在您的模板中使用它们,例如#{renderAsDate (newsItemDate entry)}。
但这不会解决运行时错误,该错误来自序列化层,并且可能独立于模板。
发布于 2012-11-16 06:08:59
我很确定你可以在村子里用show?至少我是这么做的..。
#{show $ newsItemDate entry}我以前遇到过这个实例,就像这个家伙在这里描述的那样:
作为表达式节约哲学的一部分,
Haskell不需要类型签名-尽管经验丰富的Haskeller为了清晰起见提供了类型签名-因此这种强类型语言中的类型错误对于新手来说通常是晦涩难懂的。例如,如果您定义一个函数f将两个数字相加,然后用两个字符串调用它,编译器将不会抱怨错误的参数,它会抱怨字符串不支持运算符plus。它将以一种非常不明显的方式表达这种抱怨。1在"1.哈斯克尔简明扼要“...
1
https://stackoverflow.com/questions/12275273
复制相似问题