Using BlazeHtml

It is trivial to use BlazeHtml with Happstack. Essentially you just use toResponse to convert a blaze Html value into a Response. For more detailed information on using BlazeHtml, see the BlazeHtml website. The following example should get you started:

> {-# LANGUAGE OverloadedStrings #-} > module Main where > > import Happstack.Server > import Text.Blaze ((!)) > import qualified Text.Blaze.Html4.Strict as H > import qualified Text.Blaze.Html4.Strict.Attributes as A > > appTemplate :: String -> [H.Html] -> H.Html -> H.Html > appTemplate title headers body = > H.html $ do > H.head $ do > H.title (H.toHtml title) > H.meta ! A.httpEquiv "Content-Type" ! A.content "text/html;charset=utf-8" > sequence_ headers > H.body $ do > body > > helloBlaze :: ServerPart Response > helloBlaze = > ok $ toResponse $ > appTemplate "Hello, Blaze!" > [H.meta ! A.name "keywords" ! A.content "happstack, blaze, html"] > (H.p "hello, blaze!") > > main :: IO () > main = simpleHTTP nullConf $ helloBlaze

[Source code for the app is here.]

Now if we visit http://localhost:8000/, we will get an html page which says:

hello, blaze!

This example is pretty simple, but there are a few things to note: