happstack-heist-7.2.4: Support for using Heist templates in Happstack

Safe HaskellNone



This module provides support for serving compiled Heist templates using Happstack.

The primary function provided by this module is:

heistServe :: (Happstack m) => HeistState m -> m Response

It also provides the initHeistCompiled helper function for creating a HeistState. Though you are free to use other functions from the Heist library instead.

Here is a simple example:

module Main where

import Control.Applicative    ((<$>))
import Control.Monad          (msum)
import qualified Data.Text    as T
import Happstack.Server       (dir, nullConf, nullDir, simpleHTTP, seeOther, toResponse)
import Happstack.Server.Heist (heistServe, initHeistCompiled)
import Heist                  ((##), getParamNode, noSplices)
import Heist.Compiled         (Splice, yieldRuntimeText)
import qualified Text.XmlHtml as X

-- | factorial splice
factSplice :: (Monad m) => Splice m
factSplice =
    do intStr <- T.unpack . X.nodeText <$> getParamNode
       let res = yieldRuntimeText $
                   do case reads intStr of
                        [(n,[])] ->
                            return (T.pack $ show $ product [1..(n :: Integer)])
                        _ ->
                            return (T.pack $ "Unable to parse " ++ intStr ++ " as an Integer.")
       return $ res

main :: IO ()
main =
  do heistState <- do
       r <- initHeistCompiled (T.pack "fact" ## factSplice) noSplices "."
       case r of
         (Left e) -> error $ unlines e
         (Right heistState) -> return $ heistState
     simpleHTTP nullConf $ msum
       [ dir "heist" $ heistServe heistState
       , nullDir >> seeOther "/heist/factorial" (toResponse "/heist/factorial")

It uses the following template file (factorial.tpl):

    <title>Factorial Page</title>
    <h1>Factorial Page</h1>
    <p>The factorial of 6 is <fact>6</fact></p>

For more information on using Compiled Heist Templates see:


And also see the Heist Section of the Happstack Crash Course:



initHeistCompiled Source


:: (MonadIO m, Monad n) 
=> Splices (Splice n)

compiled splices

-> Splices (AttrSplice n)

attribute splices

-> FilePath

path to template directory

-> m (Either [String] (HeistState n))