Search code examples
haskelltemplatinghappstack

Using html files as templates in happstack


I can find plenty of documentation on using blitz and other compiletime templating libraries with happstack but I would like to know how to use html files as templates.


Solution

  • Though there are many options, my favourite would be Heist, which would allow you to define a splice:

    > factSplice :: (Monad m) => TemplateMonad m Template
    > factSplice = do
    >   input <- getParamNode
    >   let text = T.unpack $ X.nodeText input
    >       n    = read text :: Int
    >   return [X.TextNode $ T.pack $ show $ product [1..n]]
    >
    

    which could be used in a dynamic (loaded at runtime) HTML template:

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

    To use heist in happstack, you'll need the happstack-heist package. For more detail and other options, see Using Heist.