2013-01-18 86 views
1

请考虑以下三个非常相似的页面。yesodweb -example和额外的标签在输出

{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes, 
     TemplateHaskell, MultiParamTypeClasses #-} 
import Yesod 
import Control.Applicative 
import Data.Text (Text) 
import Text.Hamlet 

data Example = Example 

mkYesod "Example" [parseRoutes| 
/RootR GET 
/page PageR GET 
/page2 Page2R GET 
|] 

instance Yesod Example 

getRootR :: GHandler sub Example RepHtml 
getRootR = do 
    defaultLayout [whamlet| 
$doctype 5 
<html> 
    <head> 
    <title>Tutorial, hello root 
    <body> 
    <h1 id="greeting">Hello root 
|] 


getPageR :: GHandler sub Example RepHtml 
getPageR = defaultLayout $ do 
    toWidgetHead [hamlet| <meta charset="utf-8"> |] 
    setTitle "hello page" 
    toWidget [hamlet| 
<h1 id="greetings2">Hello page 
|] 

getPage2R :: GHandler sub Example RepHtml 
getPage2R = defaultLayout $ do 
    toWidget [hamlet| 
$doctype 5 
<html> 
    <head> 
    <title>Tutorial, hello page2 
    <body> 
    <h1 id="greeting">Hello page2 
|] 


main :: IO() 
main = warpDebug 3000 Example 

RootR和第2页给出相同的输出(我的意思是标签&结构),而“页”的不同是从两个位。输出是,第一个“根” &“第2页”:

<!DOCTYPE html> 
<html><head><title></title></head><body><!DOCTYPE html> 
<html><head><title>Tutorial, hello page2</title> 
</head> 
<body><h1 id="greeting">Hello page2</h1> 
</body> 
</html> 
</body></html> 

而“页”的输出为

<!DOCTYPE html> 
<html><head><title>hello page</title><meta charset="utf-8"> </meta> 
</head><body><h1 id="greetings2">Hello page</h1> 
</body></html> 

为什么有额外的&标签都有效的“根” &“第2页”?我应该在代码中添加一些东西还是带走一些东西?

感谢您的帮助!

回答

2

default-layout函数已经包含doctype等等,并且您将在根和页面2中再次添加它。

+0

谢谢!我从书中拿了一个例子,并没有马上看到这个例子使用了其他函数(widgetToPageContent)。我会把这个加到yesod-wiki以及一个“初学者部分”来看看,如果有人愿意在学习时分享他们的“案例”。 – Gspia

+0

并且应该有hamletToRepHtml而不是widgetToPageContent – Gspia