StrappedTemplates

General purpose templates in haskell

https://github.com/hansonkd/StrappedTemplates

Latest on Hackage:0.2.0.2

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD-3-Clause licensed by Kyle Hanson
Maintained by [email protected]

Easy templating in haskell.

 import Control.Monad.IO.Class
 import qualified Blaze.ByteString.Builder as B
 import qualified Data.Text as T
 import Data.Time

 import Text.Strapped

 makeBucket :: Integer -> InputBucket IO
 makeBucket i = bucketFromList
       [ ("is", lit $ map (LitInteger) [1..i])
       , ("is_truthy", lit i)
       , ("ioTime", Func (\_ -> (liftIO $ getCurrentTime) >>= (\c -> return $ LitText $ T.pack $ show c)))
       ]

 main :: IO ()
 main = do
   tmpls <- templateStoreFromDirectory defaultConfig "examples/templates" ".strp"
   case tmpls of
     Left err -> print err
     Right store -> do
       rendered <- render (putStore store defaultConfig) (makeBucket 2) "base_simple.strp"
       either (print) (print . B.toByteString) rendered
 {$ inherits base.strp $}
 
 {$ isblock body $}
 
 An IO function to find the current time: ${ ioTime }
 
 {$ if is_truthy $}
     {$ inherits base.strp $}
     {$ isblock body $}
         Any block level can inherit from another template and override blocks.
     {$ endisblock $}
 {$ else $}
     Don't show me.
 {$ endif $}
 
 Taken from an includes:
 {$ include includes/includes.strp $}
 
 Lets count...
 {$ for i in is $}
     ${ i }
 {$ endfor $}
 
 {$ endisblock $}