[started section on templating Jeremy Shaw **20100531231940 Ignore-this: 2e95a671bd39d0f46494ed6d2555a5ae ] hunk ./Makefile 5 -DEPS := HelloWorld.lhs RouteFilters.lhs +DEPS := HelloWorld.lhs RouteFilters.lhs Templates.lhs hunk ./Makefile 19 - runhaskell gen-toc.hs > toc.html + runhaskell gen-toc.hs $^ > toc.html addfile ./Templates.lhs hunk ./Templates.lhs 1 + + + + Crash Course in Happstack + + + + + +

Back to Table of Contents

+

Templating and HTML

+ +

Happstack supports a number of third party templating and HTML +libraries. It is easy to add support for additional libraries, if your +favorite does not already have support.

+ +

Each templating system has it's own set of advantages and drawbacks.

+ +
+
HSP
+

HSP allows you to embed literal XML syntax inside your Haskell code. A pre-processor rewrites the literal XML into normal haskell function calls, and then the code is compiled.

+

pros:

+
    +
  • Templates are compiled, so they are pretty fast (needs more benchmarks to support this statement however)
  • +
  • You have the full power of Haskell in your templates, because it is Haskell (with a purely syntactic extension)
  • +
  • Type-checked at compile time to ensure types are correct and no template values are missing
  • +
  • Syntax is very similar to XML/HTML, so it is easy to learn
  • +
  • Can be easier to work with when trying to populate a template from a complex Haskell type +
  • Can be used to generate HTML or XML
  • +
+

cons:

+
    +
  • Requires you to recompile in order to update the template
  • +
  • Error messages are sometimes misleading or hard to understand
  • +
  • Makes it easy to mix the application logic and view code together, making it hard to update later (therefore you must have self control)
  • +
  • Only suitable for generating XML and HTML documents
  • +
  • Not ideal for having templates written by a non-programming web designer (although the xml syntax helps)
  • +
+
+
BlazeHtml
+
Hamlet
+
HStringTemplate
+
Heist
+
more to come..
+
+ + + + hunk ./gen-toc.hs 3 -import Data.List (groupBy) +import Control.Applicative +import Data.List (groupBy, isSuffixOf) hunk ./gen-toc.hs 9 +import System.Environment (getArgs) hunk ./gen-toc.hs 54 - do hs <- fmap concat $ mapM getHeaders files + do files <- filter (isSuffixOf ".html") <$> getArgs + hs <- fmap concat $ mapM getHeaders files