2013-01-18 85 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