Skip to content

Commit

Permalink
Updated to work with Heist 0.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
mightybyte committed Feb 5, 2011
1 parent 16ce56b commit 1bd7d9d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion snap.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Library
filepath >= 1.1 && <1.3,
MonadCatchIO-transformers >= 0.2.1 && < 0.3,
snap-core == 0.4.*,
heist >= 0.4 && < 0.5,
heist >= 0.5 && < 0.6,
hint >= 0.3.3.1 && < 0.4,
template-haskell >= 2.3 && < 2.6,
time >= 1.0 && < 1.3
Expand Down
3 changes: 2 additions & 1 deletion src/Snap/Extension/Heist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Snap.Extension.Heist
import Control.Applicative
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Text (Text)
import Snap.Types
import Snap.Util.FileServe
import Text.Templating.Heist
Expand Down Expand Up @@ -72,7 +73,7 @@ class (Monad n, MonadSnap m) => MonadHeist n m | m -> n where
renderWithSplices
:: (MonadHeist n m)
=> ByteString -- ^ Template to render
-> [(ByteString, Splice n)] -- ^ Splice mapping
-> [(Text, Splice n)] -- ^ Splice mapping
-> m ()
renderWithSplices t sps = heistLocal bsps $ render t
where bsps = bindSplices sps
42 changes: 25 additions & 17 deletions src/Snap/Extension/Heist/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ module Snap.Extension.Heist.Impl

import Control.Concurrent.MVar
import Control.Monad.Reader
import qualified Data.ByteString as B
import Data.ByteString (ByteString)
import Data.Maybe
import Data.Text (Text)
import Snap.Extension
import Snap.Extension.Heist
import Snap.Types
Expand Down Expand Up @@ -141,36 +143,42 @@ instance MonadSnap m => InitializerState (HeistState m) where

------------------------------------------------------------------------------
instance HasHeistState (SnapExtend s) s => MonadHeist (SnapExtend s) (SnapExtend s) where
render = renderAs "text/html; charset=utf-8"
render t = do
hs <- asks getHeistState
renderHelper hs Nothing t

renderAs c t = do
(HeistState _ _ tsMVar _ modifier) <- asks getHeistState
ts <- liftIO $ fmap modifier $ readMVar tsMVar
renderTemplate ts t >>= maybe pass (\ctnt -> do
modifyResponse $ setContentType c
modifyResponse $ setContentLength (fromIntegral $ B.length ctnt)
writeBS ctnt)
hs <- asks getHeistState
renderHelper hs (Just c) t

heistLocal f = local $ modifyHeistState $ \s ->
s { _modifier = f . _modifier s }


------------------------------------------------------------------------------
instance HasHeistState m s => MonadHeist m (ReaderT s m) where
render = renderAs "text/html; charset=utf-8"
render t = ReaderT $ \s -> renderHelper (getHeistState s) Nothing t

renderAs c t = ReaderT $ \s -> do
let (HeistState _ _ tsMVar _ modifier) = getHeistState s
ts <- liftIO $ fmap modifier $ readMVar tsMVar
renderTemplate ts t >>= maybe pass (\ctnt -> do
modifyResponse $ setContentType c
modifyResponse $ setContentLength (fromIntegral $ B.length ctnt)
writeBS ctnt)
renderAs c t = ReaderT $ \s -> renderHelper (getHeistState s) (Just c) t

heistLocal f = local $ modifyHeistState $ \s ->
s { _modifier = f . _modifier s }


------------------------------------------------------------------------------
renderHelper :: (MonadSnap m)
=> HeistState m
-> Maybe MIMEType
-> ByteString
-> m ()
renderHelper hs c t = do
let (HeistState _ _ tsMVar _ modifier) = hs
ts <- liftIO $ fmap modifier $ readMVar tsMVar
renderTemplate ts t >>= maybe pass (\(b,mime) -> do
modifyResponse $ setContentType $ fromMaybe mime c
writeBuilder b)


------------------------------------------------------------------------------
-- | Take your application's state and register these splices in it so
-- that you don't have to re-list them in every handler. Should be called from
Expand All @@ -193,7 +201,7 @@ registerSplices
:: (MonadSnap m, MonadIO n)
=> HeistState m
-- ^ Heist state that you are going to embed in your application's state.
-> [(B.ByteString, Splice m)]
-> [(Text, Splice m)]
-- ^ Your splices.
-> n ()
registerSplices s sps = liftIO $ do
Expand Down

0 comments on commit 1bd7d9d

Please sign in to comment.