Heist is an XML templating engine. The static HTML portions of your web pages reside in XML files which can be edited and reloaded with out having to recompile your server. The dynamic portions are generated in Haskell and spliced into the templates.
To enable Heist support, you must install the happstack-heist package. It is not installed by default.
The following template is almost an XHTML document, except that it contains the special tag <fact>6</fact>: #ifdef HsColour
#include "factorial.tpl-inc"
The <fact/> tag is an application specific tag which performs a factorial and splices in the result.
The following example shows how to initialize the Heist template system and how to create your own custom tags.
First a bunch of boring imports:
Next we have the factorial splice:
The splice runs in the Splice
monad transformer, which is a type name for HeistT
:
This transformer provides access to node information using the getParamNode
function:
It returns the XHTML node that triggered this splice function to be called. In this case it would be, <fact>6</fact>
In our main function, we must first initialize the Heist template system by using initHeistCompiled
, passing in an array of all the custom splices we are providing. In this example, we would put the templates in same directory the app is running from:
to serve templates we simply use the heistServe
function:
heistServe
will look at the path in the URL, add .tpl to the end, and try to find a matching template file on disk to return.
Because the templates are loaded into memory, updating the files on disk will not have any immediate effect. You will need to restart your server.
[Source code for the app is here. You will also need to download the source for factorial.tpl and save it in the same directory as TemplateHeist.hs.]
If you point your browser at http://localhost:8000/ you should see the factorial page.
Heist offers a variety of other features not shown here. We have only covered the Happstack integration aspects. For more general information on Heist look at the Official Heist Template Tutorial and the Heist Haddock Documenation.