yesod博客示例编译错误

发布于 2024-12-05 09:52:15 字数 2547 浏览 0 评论 0原文

{-# 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-12-12 09:52:15

最新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文