---
This patch applies on top of heist version 0.5.0.1.
In trying to work out the Heist example on
http://hackage.haskell.org/packages/archive/heist/0.5.0.1/doc/html/Text-Templating-Heist.html
I've changed the code slightly to make it work with the missing
definitions below.
newtype MyAppMonad a = MyAppMonad { unMyAppMonad :: (ReaderT MyAppData IO
a) } deriving (Monad, MonadIO)
data MyAppData = MyAppData { user :: Text }
{- Format of file 'userdb':
Dogbert
anonymous
-}
runMyAppMonad :: MyAppMonad a -> IO a
runMyAppMonad m = do
s <- readFile "userdb"
let myData = MyAppData $ T.pack $ head $ lines s
runReaderT (unMyAppMonad m) myData
getUser :: MyAppMonad (Maybe Text)
getUser = MyAppMonad $ do
appData <- ask
return $ if (user appData) == "anonymous"
then Nothing
else Just (user appData)
Since there is a convenient function 'bindSplices', I've used that too.
src/Text/Templating/Heist.hs | 13 +++++++------
1 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/src/Text/Templating/Heist.hs b/src/Text/Templating/Heist.hs
index 17349e0..d3cb298 100644
--- a/src/Text/Templating/Heist.hs
+++ b/src/Text/Templating/Heist.hs
@@ -22,6 +22,7 @@
@getUser :: MyAppMonad (Maybe Text)@ that gets the current user.
You can implement this functionality with a 'Splice' as follows:
+ > import Blaze.ByteString.Builder
> import Data.ByteString.Char8 (ByteString)
> import qualified Data.ByteString.Char8 as B
> import Data.Text (Text)
@@ -30,19 +31,19 @@
>
> import Text.Templating.Heist
>
- > link :: Text -> Text -> Node
+ > link :: Text -> Text -> X.Node
> link target text = X.Element "a" [("href", target)] [X.TextNode text]
>
- > loginLink :: Node
+ > loginLink :: X.Node
> loginLink = link "/login" "Login"
>
- > logoutLink :: Text -> Node
+ > logoutLink :: Text -> X.Node
> logoutLink user = link "/logout" (T.append "Logout " user)
>
> loginLogoutSplice :: Splice MyAppMonad
> loginLogoutSplice = do
> user <- lift getUser
- > return $ [maybe loginLink logoutLink user]
+ > return [maybe loginLink logoutLink user]
>
Next, you need to bind that splice to a tag. Heist stores information
@@ -53,10 +54,10 @@
>
> main = do
> ets <- loadTemplates "templates" $
- > foldr (uncurry bindSplice) emptyTemplateState mySplices
+ > bindSplices mySplices (emptyTemplateState "templates")
> let ts = either error id ets
> t <- runMyAppMonad $ renderTemplate ts "index"
- > print $ maybe "Page not found" id t
+ > print $ maybe "Page not found" (toByteString . fst) t
Here we build up our 'TemplateState' by starting with
emptyTemplateState and
applying bindSplice for all the splices we want to add. Then we pass this
--
1.7.4
_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap