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.


  1. import Network.CGI  
  2. import Text.XHtml  
  3.   
  4. inputForm :: Html  
  5. inputForm = form << [paragraph << "Who are you?",  
  6.                      textfield "name",  
  7.                      submit "" "send"]  
  8.   
  9. greet :: String -> Html  
  10. greet n = h1 << ("Hello, " ++ n ++ "!")  
  11.   
  12. page :: String -> Html -> Html  
  13. page t b = header << thetitle << t +++ body << b  
  14.   
  15. cgiMain :: CGI CGIResult  
  16. cgiMain = do name <- getInput "name"  
  17.              let x = maybe inputForm greet name  
  18.              output $ renderHtml $ page "Hello" x  
  19.   
  20. main :: IO ()  
  21. main = runCGI $ handleErrors cgiMain  



This code is simpler than the previous code.

No comments: