使用UTCTime和Hamlet [英] Using UTCTime with Hamlet

查看:128
本文介绍了使用UTCTime和Hamlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NewsItem 
日期UTCTime default = CURRENT_TIME
title字符串
内容字符串
作者字符串



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

并最终在我的模板中使用:

  $如果为空newsitems 

没有消息。
$ else
$ forall实体ID条目< - newsitems
<文章>
< h4>#{newsItemDate entry}
< p>#{newsItemContent entry}

但是我得到一个有关数据类型的错误:

  Handler / Home.hs:20:11:
没有使用`toHtml'
引起的
的实例(Text.Blaze.ToMarkup
time-1.4:Data.Time.Clock.UTC.UTCTime)可能的修正:

添加一个实例声明(Text.Blaze.ToMarkup time-1.4:Data.Time.Clock.UTC.UTCTime)
在`toWidget'的第一个参数中,即
`toHtml (newsItemDate entry_a6ev)'
在'do'块的标记中:
toWidget(toHtml(newsItemDate entry_a6ev))
在表达式中:
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));
....}

所以我想我会继续添加到我的导入.hs:

$ $ p $ import Data.Time(UTCTime)
import Data.Time.Format(formatTime)
导入Text.Blaze(ToMarkup,toMarkup)
导入Text.Blaze.Internal(字符串)
导入System.Locale(defaultTimeLocale)

- 格式日期为2012年7月26日
实例ToMarkup UTCTime其中
toMarkup a = string(formatTime defaultTimeLocale%e%B%Ya)

哪个编译,但在浏览器的运行时会给我一个错误:

 内部服务器错误
PersistMarshalErrorExpected UTCTime,received PersistText \2012-08-30 \



<所以我不知道如何解决这个,任何想法?编辑:源代码的网站,以防万一它是需要或好奇: https://github.com/iaefai/socrsite

解决方案

没有调查实际的错误,我认为你的方法并不好。毕竟,你很可能最终会希望采用几种格式化 UTCTime 的方法来存储时间,而不仅仅是日期。通过给出 ToMarkup UTCTime 实例,您可以在全局范围内解决此问题。



我建议编写函数 renderAsDate :: UTCDate - > HTML renderAsTime :: UTCDate - > HTML 等,并在您的模板中使用它们,例如#{renderAsDate(newsItemDate entry)}



但是这并不能解决运行时错误,序列化层,并可能独立于您的模板。


I am using Yesod on my first site and I have a list of news items:

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

which are retrieved in my handler:

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

and 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 datatypes:

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 figure I would go ahead 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 does compile, but gives me an error at runtime in the browser:

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

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

EDIT: Source code to the site in case it is needed or curious: https://github.com/iaefai/socrsite

解决方案

Without investigating the actual error, I think your approach is not great. You will very likely eventually want several ways of formatting a UTCTime, after all, the type is there to store times, not just dates. By giving a ToMarkup UTCTime instance, you fix this globally.

I would recommend to write functions renderAsDate :: UTCDate -> HTML, renderAsTime :: UTCDate -> HTML etc. and use them in your template, e.g. #{renderAsDate (newsItemDate entry)}.

But this won’t solve the runtime error, which comes from the serialization layer and is likely independent of your templates.

这篇关于使用UTCTime和Hamlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆