ホーム > Haskell > Yesodで動的サイトを作成する >

Yesodでサブサイトを作成する

プログラム例

main.hs

{-# LANGUAGE QuasiQuotes     #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

import Yesod
import SubSite

data Master = Master
    { getSubSite :: SubSite
    }

mkYesod "Master" [parseRoutes|
/    RootR    GET
/sub SubSiteR SubSite getSubSite
|]

instance Yesod Master

getRootR :: HandlerT Master IO Html
getRootR = defaultLayout [whamlet|
<h1>Welcome to the homepage
<p>
  <a href=@{SubSiteR SubRootR}>Go to subsite
|]

main :: IO ()
main = warp 3000 $ Master SubSite

SubSite.hs

{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}

module SubSite
    ( module SubSite.Data
    , module SubSite
    ) where

import Yesod
import SubSite.Data

getSubRootR :: (Yesod master) => HandlerT SubSite (HandlerT master IO) Html
getSubRootR = do
  toParent <- getRouteToParent
  lift $ defaultLayout [whamlet|
<p>
  Welcome to the subsite!
<p>
  <a href=@{toParent SubPostR}>View post.
|]

getSubPostR :: Yesod master => HandlerT SubSite (HandlerT master IO) Html
getSubPostR = lift $ defaultLayout [whamlet|This is post page.|]

instance Yesod master => YesodSubDispatch SubSite (HandlerT master IO) where
    yesodSubDispatch = $(mkYesodSubDispatch resourcesSubSite)

SubSite/Data.hs

{-# LANGUAGE QuasiQuotes     #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

module SubSite.Data where

import Yesod

data SubSite = SubSite

mkYesodSubData "SubSite" [parseRoutes|
/     SubRootR GET
/post SubPostR GET
|]