You will need to install the optional web-routes, web-routes-th, web-routes-hsp and happstack-hsp packages for this section.
> {-# LANGUAGE TemplateHaskell #-}
> {-# OPTIONS_GHC -F -pgmFtrhsx #-}
> module Main where
>
> import Control.Applicative ((<$>))
> import Happstack.Server
> import Happstack.Server.HSP.HTML
> import qualified HSX.XMLGenerator as HSX
> import Web.Routes
> import Web.Routes.TH
> import Web.Routes.XMLGenT
> import Web.Routes.Happstack
If you are using web-routes and HSP then inserting URLs is especially clean and easy. If we have the URL:
> data SiteURL = Monkeys Int deriving (Eq, Ord, Read, Show)
>
> $(derivePathInfo ''SiteURL)
>
Now we can define a template like this:
> monkeys :: Int -> RouteT SiteURL (ServerPartT IO) Response
> monkeys n =
> do html <- defaultTemplate "monkeys" () $
> <%>
>
You have <% show n %> monkeys.
>
Click here for more.
> %>
> ok $ (toResponse html)
Notice that in particular this bit:
#ifdef HsColour
>
here
#endif
> route :: SiteURL -> RouteT SiteURL (ServerPartT IO) Response
> route url =
> case url of
> (Monkeys n) -> monkeys n
[Source code for the app is here.]