Widget is a Writer

This commit is contained in:
Michael Snoyman 2011-12-06 13:58:41 +02:00
parent a4346b3ea7
commit d39e0d9bfd
2 changed files with 12 additions and 13 deletions

View File

@ -46,7 +46,7 @@ import qualified Web.ClientSession as CS
import qualified Data.ByteString.Char8 as S8 import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.Monoid import Data.Monoid
import Control.Monad.Trans.RWS import Control.Monad.Trans.Writer (runWriterT)
import Text.Hamlet import Text.Hamlet
import Text.Julius import Text.Julius
import Text.Blaze ((!), customAttribute, textTag, toValue, unsafeLazyByteString) import Text.Blaze ((!), customAttribute, textTag, toValue, unsafeLazyByteString)
@ -501,7 +501,7 @@ widgetToPageContent :: (Eq (Route master), Yesod master)
-> GHandler sub master (PageContent (Route master)) -> GHandler sub master (PageContent (Route master))
widgetToPageContent (GWidget w) = do widgetToPageContent (GWidget w) = do
master <- getYesod master <- getYesod
((), _, GWData (Body body) (Last mTitle) scripts' stylesheets' style jscript (Head head')) <- runRWST w () 0 ((), GWData (Body body) (Last mTitle) scripts' stylesheets' style jscript (Head head')) <- runWriterT w
let title = maybe mempty unTitle mTitle let title = maybe mempty unTitle mTitle
let scripts = runUniqueList scripts' let scripts = runUniqueList scripts'
let stylesheets = runUniqueList stylesheets' let stylesheets = runUniqueList stylesheets'

View File

@ -57,7 +57,7 @@ module Yesod.Widget
) where ) where
import Data.Monoid import Data.Monoid
import Control.Monad.Trans.RWS import Control.Monad.Trans.Writer
import qualified Text.Blaze.Html5 as H import qualified Text.Blaze.Html5 as H
import Text.Hamlet import Text.Hamlet
import Text.Cassius import Text.Cassius
@ -103,10 +103,11 @@ instance MonadBase b m => MonadBase b (GGWidget master m) where
liftBase = lift . liftBase liftBase = lift . liftBase
#if MIN_VERSION_monad_control(0, 3, 0) #if MIN_VERSION_monad_control(0, 3, 0)
instance MonadTransControl (GGWidget master) where instance MonadTransControl (GGWidget master) where
newtype StT (GGWidget master) a = StRWS {unStRWS :: (a, Int, GWData (Route master))} newtype StT (GGWidget master) a =
liftWith f = GWidget $ RWST $ \r s -> liftM (\x -> (x, s, mempty)) StWidget {unStWidget :: StT (GWInner master) a}
(f $ \t -> liftM StRWS $ runRWST (unGWidget t) r s) liftWith f = GWidget $ liftWith $ \run ->
restoreT mSt = GWidget $ RWST $ \_ _ -> liftM unStRWS mSt f $ liftM StWidget . run . unGWidget
restoreT = GWidget . restoreT . liftM unStWidget
{-# INLINE liftWith #-} {-# INLINE liftWith #-}
{-# INLINE restoreT #-} {-# INLINE restoreT #-}
instance MonadBaseControl b m => MonadBaseControl b (GGWidget master m) where instance MonadBaseControl b m => MonadBaseControl b (GGWidget master m) where
@ -119,7 +120,7 @@ instance MonadTrans (GGWidget m) where
lift = GWidget . lift lift = GWidget . lift
type GWidget s m = GGWidget m (GHandler s m) type GWidget s m = GGWidget m (GHandler s m)
type GWInner master = RWST () (GWData (Route master)) Int type GWInner master = WriterT (GWData (Route master))
instance (Monad monad, a ~ ()) => Monoid (GGWidget master monad a) where instance (Monad monad, a ~ ()) => Monoid (GGWidget master monad a) where
mempty = return () mempty = return ()
@ -129,9 +130,7 @@ addSubWidget :: (YesodSubRoute sub master) => sub -> GWidget sub master a -> GWi
addSubWidget sub (GWidget w) = do addSubWidget sub (GWidget w) = do
master <- lift getYesod master <- lift getYesod
let sr = fromSubRoute sub master let sr = fromSubRoute sub master
s <- GWidget get (a, w') <- lift $ toMasterHandlerMaybe sr (const sub) Nothing $ runWriterT w
(a, s', w') <- lift $ toMasterHandlerMaybe sr (const sub) Nothing $ runRWST w () s
GWidget $ put s'
GWidget $ tell w' GWidget $ tell w'
return a return a
@ -299,9 +298,9 @@ addCoffeeBody c = do
-- manipulations. It can be easier to use this sometimes than 'wrapWidget'. -- manipulations. It can be easier to use this sometimes than 'wrapWidget'.
extractBody :: Monad mo => GGWidget m mo () -> GGWidget m mo (HtmlUrl (Route m)) extractBody :: Monad mo => GGWidget m mo () -> GGWidget m mo (HtmlUrl (Route m))
extractBody (GWidget w) = extractBody (GWidget w) =
GWidget $ mapRWST (liftM go) w GWidget $ mapWriterT (liftM go) w
where where
go ((), s, GWData (Body h) b c d e f g) = (h, s, GWData (Body mempty) b c d e f g) go ((), GWData (Body h) b c d e f g) = (h, GWData (Body mempty) b c d e f g)
-- | Content for a web page. By providing this datatype, we can easily create -- | Content for a web page. By providing this datatype, we can easily create
-- generic site templates, which would have the type signature: -- generic site templates, which would have the type signature: