\begin{code}
{-# LANGUAGE QuasiQuotes, OverloadedStrings #-}
import Text.Hamlet
import Data.Text (Text, cons)
import qualified Data.Text.Lazy.IO as L
import Text.Blaze.Renderer.Text (renderHtml)
data Person = Person
{ name :: String
, age :: String
, page :: PersonUrls
, isMarried :: Bool
, children :: [String]
}
data PersonUrls = Homepage | PersonPage Text
renderUrls :: PersonUrls -> [(Text, Text)] -> Text
renderUrls Homepage _ = "/"
renderUrls (PersonPage name') _ = '/' `cons` name'
footer :: HtmlUrl url
footer = [hamlet|
<div id="footer">Thank you, come again
|]
template :: Person -> HtmlUrl PersonUrls
template person = [hamlet|
!!!
<html>
<head>
<title>Hamlet Demo
<body>
<h1>Information on #{name person}
<p>#{name person} is #{age person} years old.
<h2>
$if isMarried person
\Married
$else
\Not married
<ul>
$forall child <- children person
<li>#{child}
<p>
<a href="@{page person}">See the page.
\^{footer}
|]
main :: IO ()
main = do
let person = Person
{ name = "Michael"
, age = "twenty five & a half"
, page = PersonPage "michael"
, isMarried = True
, children = ["Adam", "Ben", "Chris"]
}
L.putStrLn $ renderHtml $ (template person) renderUrls
\end{code}
Outputs (new lines added for readability):
<code><pre>
<!DOCTYPE html>
<html><head><title>Hamlet Demo</title></head><body>
<h1>Information on Michael</h1>
<p>Michael is twenty five & a half years old.</p>
<h2>Married</h2>
<ul><li>Adam</li><li>Ben</li><li>Chris</li></ul>
<p><a href="/michael">See the page.</a></p>
<div id="footer">Thank you, come again</div>
</body></html>
</pre></code>