Documented Yesod.Yesod

This commit is contained in:
Michael Snoyman 2010-04-23 12:35:36 -07:00
parent 3701e3c490
commit c320d2a45b

View File

@ -2,10 +2,14 @@
{-# LANGUAGE RankNTypes #-} {-# LANGUAGE RankNTypes #-}
-- | The basic typeclass for a Yesod application. -- | The basic typeclass for a Yesod application.
module Yesod.Yesod module Yesod.Yesod
( Yesod (..) ( -- * Type classes
Yesod (..)
, YesodSite (..) , YesodSite (..)
-- * Convenience functions
, applyLayout , applyLayout
, applyLayoutJson , applyLayoutJson
-- * Defaults
, defaultErrorHandler
) where ) where
import Yesod.Content import Yesod.Content
@ -22,10 +26,18 @@ import Yesod.Internal
import Web.Routes.Quasi (QuasiSite (..)) import Web.Routes.Quasi (QuasiSite (..))
-- | This class is automatically instantiated when you use the template haskell
-- mkYesod function.
class YesodSite y where class YesodSite y where
getSite :: QuasiSite YesodApp y y getSite :: QuasiSite YesodApp y y
-- | Define settings for a Yesod applications. The only required setting is
-- 'approot'; other than that, there are intelligent defaults.
class YesodSite a => Yesod a where class YesodSite a => Yesod a where
-- | An absolute URL to the root of the application. Do not include
-- trailing slash.
approot :: a -> Approot
-- | The encryption key to be used for encrypting client sessions. -- | The encryption key to be used for encrypting client sessions.
encryptKey :: a -> IO Word256 encryptKey :: a -> IO Word256
encryptKey _ = getKey defaultKeyFile encryptKey _ = getKey defaultKeyFile
@ -42,12 +54,12 @@ class YesodSite a => Yesod a where
-> Handler y ChooseRep -> Handler y ChooseRep
errorHandler _ = defaultErrorHandler errorHandler _ = defaultErrorHandler
-- | Applies some form of layout to <title> and <body> contents of a page. FIXME: use a Maybe here to allow subsites to simply inherit. -- | Applies some form of layout to <title> and <body> contents of a page.
rawApplyLayout :: a defaultLayout :: a
-> PageContent (Routes a) -> PageContent (Routes a)
-> Request -> Request
-> Hamlet (Routes a) IO () -> Hamlet (Routes a) IO ()
rawApplyLayout _ p _ = [$hamlet| defaultLayout _ p _ = [$hamlet|
!!! !!!
%html %html
%head %head
@ -61,11 +73,7 @@ class YesodSite a => Yesod a where
onRequest :: a -> Request -> IO () onRequest :: a -> Request -> IO ()
onRequest _ _ = return () onRequest _ _ = return ()
-- | An absolute URL to the root of the application. Do not include -- | Apply the default layout ('defaultLayout') to the given title and body.
-- trailing slash.
approot :: a -> Approot
-- | A convenience wrapper around 'simpleApplyLayout for HTML-only data.
applyLayout :: Yesod master applyLayout :: Yesod master
=> String -- ^ title => String -- ^ title
-> Hamlet (Routes master) IO () -- ^ body -> Hamlet (Routes master) IO () -- ^ body
@ -78,9 +86,11 @@ applyLayout t b = do
} }
y <- getYesodMaster y <- getYesodMaster
rr <- getRequest rr <- getRequest
content <- hamletToContent $ rawApplyLayout y pc rr content <- hamletToContent $ defaultLayout y pc rr
return $ RepHtml content return $ RepHtml content
-- | Provide both an HTML and JSON representation for a piece of data, using
-- the default layout for the HTML output ('defaultLayout').
applyLayoutJson :: Yesod master applyLayoutJson :: Yesod master
=> String -- ^ title => String -- ^ title
-> x -> x
@ -95,7 +105,7 @@ applyLayoutJson t x toH toJ = do
} }
y <- getYesodMaster y <- getYesodMaster
rr <- getRequest rr <- getRequest
html <- hamletToContent $ rawApplyLayout y pc rr html <- hamletToContent $ defaultLayout y pc rr
json <- jsonToContent $ toJ x json <- jsonToContent $ toJ x
return $ RepHtmlJson html json return $ RepHtmlJson html json
@ -105,6 +115,7 @@ applyLayout' :: Yesod master
-> GHandler sub master ChooseRep -> GHandler sub master ChooseRep
applyLayout' s = fmap chooseRep . applyLayout s applyLayout' s = fmap chooseRep . applyLayout s
-- | The default error handler for 'errorHandler'.
defaultErrorHandler :: Yesod y defaultErrorHandler :: Yesod y
=> ErrorResponse => ErrorResponse
-> Handler y ChooseRep -> Handler y ChooseRep