-
Notifications
You must be signed in to change notification settings - Fork 28
/
2013-12-11-heist.hs
60 lines (44 loc) · 1.59 KB
/
2013-12-11-heist.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE OverloadedStrings #-}
import Blaze.ByteString.Builder (toByteString)
import qualified Data.ByteString.Char8 as BS
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Trans.Either
import Data.Monoid (mempty)
import Data.Foldable (forM_)
import Heist
import Heist.Interpreted
import Text.XmlHtml (Node(TextNode), renderHtmlFragment, Encoding(UTF8))
billy :: IO ()
billy = eitherT (putStrLn . unlines) return $ do
heist <- initHeist mempty
{ hcTemplateLocations = [ loadTemplates "templates" ]
, hcInterpretedSplices = defaultInterpretedSplices
}
Just (output, _) <- renderTemplate heist "billy"
liftIO . BS.putStrLn . toByteString $ output
names :: IO ()
names = eitherT (putStrLn . unlines) return $ do
heist <- initHeist mempty
{ hcTemplateLocations = [ loadTemplates "templates" ]
, hcInterpretedSplices = defaultInterpretedSplices
}
names <- liftIO getNames
forM_ names $ \name -> do
Just (output, _) <- renderTemplate
(bindSplice "kiddo" (textSplice name) heist)
"merry-christmas"
liftIO . BS.putStrLn . toByteString $ output
getNames = return [ "Tom", "Dick", "Harry" ]
summary :: IO ()
summary = eitherT (putStrLn . unlines) return $ do
heist <- initHeist mempty
{ hcTemplateLocations = [ loadTemplates "templates" ]
, hcInterpretedSplices = defaultInterpretedSplices
}
Just (output, _) <- renderTemplate
(bindSplice "names" namesSplice heist)
"summary"
liftIO . BS.putStrLn . toByteString $ output
namesSplice =
liftIO getNames >>=
mapSplices (\name -> runChildrenWithText ("name" ## name))