Text.HtmlのXHTML版で使い方はほぼ同じです。Here are some notes on using the HTML librariesが参考になります。
NewCGIと組み合わせて使うとこんな感じです。
{-# OPTIONS_GHC -fallow-overlapping-instances #-} import Network.NewCGI import Text.XHtml main :: IO () main = runCGI (handleErrors cgiMain) cgiMain :: CGI CGIResult cgiMain = do setHeader "Content-Type" "text/html; charset=utf-8" output $ renderHtml page where page = header << (meta ! [httpequiv "content-type", content "text/html; charset=utf-8"] +++ thetitle << "Test") +++ body << p << "Hello, world!"
ついでにCGIの入力を使うならこんな感じです。
{-# OPTIONS_GHC -fallow-overlapping-instances #-} import Data.Maybe import Network.NewCGI import Text.XHtml main :: IO () main = runCGI (handleErrors cgiMain) cgiMain :: CGI CGIResult cgiMain = do setHeader "Content-Type" "text/html; charset=utf-8" name <- getInput "name" output $ renderHtml $ page $ fromMaybe "Guest" name where page name = header << (meta ! [httpequiv "content-type", content "text/html; charset=utf-8"] +++ thetitle << "Test") +++ body << p << ("Hello, " ++ name ++ "!")
ちなみに、デフォルトだと日本語がうまく使えない*1ので、以下のパッチを当てて無理やり通してしまいます。
diff -ur xhtml-2006.7.5.orig/Text/XHtml.hs xhtml-2006.7.5/Text/XHtml.hs --- xhtml-2006.7.5.orig/Text/XHtml.hs 2006-07-05 22:27:18.000000000 -0400 +++ xhtml-2006.7.5/Text/XHtml.hs 2006-08-17 09:19:34.000000000 -0400 @@ -179,8 +179,9 @@ fixChar '>' = ">" fixChar '&' = "&" fixChar '"' = """ - fixChar c | isAscii c && (isPrint c || isSpace c) = [c] - fixChar c = "&#" ++ show (ord c) ++ ";" +-- fixChar c | isAscii c && (isPrint c || isSpace c) = [c] +-- fixChar c = "&#" ++ show (ord c) ++ ";" + fixChar c = [c] -- --------------------------------------------------------------------------- -- Classes
*1 というより実際にはUnicodeの文字列が入るべきStringに無理やりutf-8のバイト列を押し込んでいるのが原因なのですが…