Oct 12, 2009

Making Web Application with Haskell #3

I make a web application with Haskell.
It's a simple application.
You input your name, then the application output your name.
See below.






The other day I made the application with Network.CGI.
Today I make it with Text.XHtml wich is included in GHC.



import Network.CGI
import Text.XHtml

inputForm :: Html
inputForm = form << [paragraph << "Who are you?",
textfield "name",
submit "" "send"]

greet :: String -> Html
greet n = h1 << ("Hello, " ++ n ++ "!")

page :: String -> Html -> Html
page t b = header << thetitle << t +++ body << b

cgiMain :: CGI CGIResult
cgiMain = do name <- getInput "name"
let x = maybe inputForm greet name
output $ renderHtml $ page "Hello" x

main :: IO ()
main = runCGI $ handleErrors cgiMain



This code is simpler than the previous code.

No comments: