yesod博客示例编译错误
{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
import Yesod
data Entry = Entry
{ entryTitle :: String
, entrySlug :: String -- ^ used in the URL
, entryContent :: String
}
loadEntries :: IO [Entry]
loadEntries = return
[ Entry "Entry 1" "entry-1" "My first entry"
, Entry "Entry 2" "entry-2" "My second entry"
, Entry "Entry 3" "entry-3" "My third entry"
]
data Blog = Blog { blogEntries :: [Entry] }
type Handler = GHandler Blog Blog
mkYesod "Blog" [parseRoutes|
/ HomeR GET
/entry/#String EntryR GET
|]
instance Yesod Blog where approot _ = ""
getHomeR :: Handler ()
getHomeR = do
Blog entries <- getYesod
let newest = last entries
redirect RedirectTemporary $ EntryR $ entrySlug newest
data TemplateArgs = TemplateArgs
{ templateTitle :: Html
, templateContent :: Html
, templateNavbar :: [Nav]
}
data Nav = Nav
{ navUrl :: Route Blog
, navTitle :: Html
}
entryTemplate :: TemplateArgs -> Hamlet (Route Blog)
entryTemplate args = [hamlet|
!!!
<html>
<head>
<title>#{templateTitle args}
<body>
<h1>Yesod Sample Blog
<h2>#{templateTitle args}
<ul id="nav">
$forall nav <- templateNavbar args
<li>
<a href="@{navUrl nav}">#{navTitle nav}
<div id="content">
\#{templateContent args}
|]
getEntryR :: String -> Handler RepHtml
getEntryR slug = do
Blog entries <- getYesod
case filter (\e -> entrySlug e == slug) entries of
[] -> notFound
(entry:_) -> do
let nav = reverse $ map toNav entries
let tempArgs = TemplateArgs
{ templateTitle = toHtml $ entryTitle entry
, templateContent = toHtml $ entryContent entry
, templateNavbar = nav
}
hamletToRepHtml $ entryTemplate tempArgs
where
toNav :: Entry -> Nav
toNav e = Nav
{ navUrl = EntryR $ entrySlug e
, navTitle = toHtml $ entryTitle e
}
main :: IO ()
main = do
entries <- loadEntries
warpDebug 3000 $ Blog entries
$ runghc Blog.hs
Blog.hs:21:1:
Multiple declarations of `Handler'
Declared at: Blog.hs:19:6
Blog.hs:21:1
{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
import Yesod
data Entry = Entry
{ entryTitle :: String
, entrySlug :: String -- ^ used in the URL
, entryContent :: String
}
loadEntries :: IO [Entry]
loadEntries = return
[ Entry "Entry 1" "entry-1" "My first entry"
, Entry "Entry 2" "entry-2" "My second entry"
, Entry "Entry 3" "entry-3" "My third entry"
]
data Blog = Blog { blogEntries :: [Entry] }
type Handler = GHandler Blog Blog
mkYesod "Blog" [parseRoutes|
/ HomeR GET
/entry/#String EntryR GET
|]
instance Yesod Blog where approot _ = ""
getHomeR :: Handler ()
getHomeR = do
Blog entries <- getYesod
let newest = last entries
redirect RedirectTemporary $ EntryR $ entrySlug newest
data TemplateArgs = TemplateArgs
{ templateTitle :: Html
, templateContent :: Html
, templateNavbar :: [Nav]
}
data Nav = Nav
{ navUrl :: Route Blog
, navTitle :: Html
}
entryTemplate :: TemplateArgs -> Hamlet (Route Blog)
entryTemplate args = [hamlet|
!!!
<html>
<head>
<title>#{templateTitle args}
<body>
<h1>Yesod Sample Blog
<h2>#{templateTitle args}
<ul id="nav">
$forall nav <- templateNavbar args
<li>
<a href="@{navUrl nav}">#{navTitle nav}
<div id="content">
\#{templateContent args}
|]
getEntryR :: String -> Handler RepHtml
getEntryR slug = do
Blog entries <- getYesod
case filter (\e -> entrySlug e == slug) entries of
[] -> notFound
(entry:_) -> do
let nav = reverse $ map toNav entries
let tempArgs = TemplateArgs
{ templateTitle = toHtml $ entryTitle entry
, templateContent = toHtml $ entryContent entry
, templateNavbar = nav
}
hamletToRepHtml $ entryTemplate tempArgs
where
toNav :: Entry -> Nav
toNav e = Nav
{ navUrl = EntryR $ entrySlug e
, navTitle = toHtml $ entryTitle e
}
main :: IO ()
main = do
entries <- loadEntries
warpDebug 3000 $ Blog entries
$ runghc Blog.hs
Blog.hs:21:1:
Multiple declarations of `Handler'
Declared at: Blog.hs:19:6
Blog.hs:21:1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最新的 Yesod 现在自动声明 Handler 类型别名。只需删除
type Handler = ...
行就可以了。我会更新这本书,我想我的检查脚本还不能在 Literate Haskell 代码上运行。The newest Yesod now automatically declares the Handler type alias. Just remove the
type Handler = ...
line and it should work. I'll get the book updated, I guess my check script isn't working on the Literate Haskell code yet.