[bunch of updates to the crash course Jeremy Shaw **20100721185247 Ignore-this: 569aecf07d7ed3b850cccdbc0b7f1e44 ] addfile ./HelloBlaze.lhs hunk ./HelloBlaze.lhs 1 + + +

Using BlazeHtml

+

The contents of this section are based on the development version of Happstack which includes new functionality

+

It is trivial to use BlazeHtml with Happstack. Essentially you just use toResponse to convert the blaze Html () type into a Response. For more detailed information on using BlazeHtml, see the BlazeHtml website. The following example should get you started:

+ +
+ +> {-# LANGUAGE OverloadedStrings #-} +> module Main where +> +> import Happstack.Server +> import Text.Blaze ((!)) +> import qualified Text.Blaze.Html4.Strict as H +> import qualified Text.Blaze.Html4.Strict.Attributes as A +> +> appTemplate :: String -> [H.Html ()] -> H.Html () -> H.Html () +> appTemplate title headers body = +> H.html $ do +> H.head $ do +> H.title (H.string title) +> H.meta ! A.http_equiv "Content-Type" ! A.content "text/html;charset=utf-8" +> sequence headers +> H.body $ do +> body +> +> helloBlaze :: ServerPart Response +> helloBlaze = +> ok $ toResponse $ +> appTemplate "Hello, Blaze!" +> [H.meta ! A.name "keywords" ! A.content "happstack, blaze, html"] +> (H.p "hello, blaze!") +> +> main :: IO () +> main = simpleHTTP nullConf $ helloBlaze + +
+

[Source code for the app is here.]

+ +

Now if we visit http://localhost:8000/, we will get an html page which says: + +

+hello ,blaze!
+
+ +

This example is pretty simple, but there are a few things to +note:

+ + hunk ./HelloWorld.lhs 35 +

To build the application run:

+ +
+ $ ghc --make -threaded HelloWorld.hs -o helloworld +
+ +

The executable will be named helloworld.

+ +

Alternatively, you can use runhaskell and avoid the compilation step.

+ +
+ $ runhaskell HelloWorld.hs +
+ hunk ./HelloWorld.lhs 55 -
- happstack-crashcourse $ curl -v http://localhost:8000/
- * About to connect() to localhost port 8000 (#0)
- *   Trying ::1... Connection refused
- *   Trying 127.0.0.1... connected
- * Connected to localhost (127.0.0.1) port 8000 (#0)
- > GET / HTTP/1.1
- > User-Agent: curl/7.19.5 (i486-pc-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.10 libssh2/0.18
- > Host: localhost:8000
- > Accept: */*
- > 
- < HTTP/1.1 200 OK
- < Connection: Keep-Alive
- < Content-Length: 13
- < Content-Type: text/plain; charset=UTF-8
- < Date: Fri, 09 Apr 2010 14:47:10 GMT
- < Server: Happstack/0.4.3
- < 
- * Connection #0 to host localhost left intact
- * Closing connection #0
+
+  $ curl http://localhost:8000/
hunk ./HelloWorld.lhs 58
-
+
hunk ./Makefile 8 -RQDATA_DEPS := RqDataLimiting.lhs -RQDATA_DEMOS := HelloRqData.lhs RqDataError.lhs RqDataUpload.lhs -DEMOS := $(RQDATA_DEMOS) $(ROUTE_FILTER_DEMOS) $(HELLOWORLD_DEMOS) +RQDATA_DEPS := RqDataLimiting.lhs RqDataParsing.lhs +RQDATA_DEMOS := HelloRqData.lhs RqDataError.lhs RqDataUpload.lhs RqDataRead.lhs RqDataFromData.lhs RqDataCheck.lhs RqDataCheckOther.lhs +TEMPLATES_DEPS := +TEMPLATES_DEMOS := HelloBlaze.lhs +DEMOS := $(RQDATA_DEMOS) $(ROUTE_FILTER_DEMOS) $(HELLOWORLD_DEMOS) $(TEMPLATES_DEPS) $(TEMPLATES_DEMOS) hunk ./Makefile 14 -SRC := $(MAIN) $(DEPS) $(ROUTE_FILTER_DEPS) +SRC := $(MAIN) $(DEPS) $(ROUTE_FILTER_DEPS) $(DEMOS) hunk ./Makefile 22 +Templates.html : $(TEMPLATES_DEPS) $(TEMPLATES_DEMOS) hunk ./RqData.lhs 25 +#include "RqDataParsing.lhs" hunk ./RqDataError.lhs 37 -> badRequest $ unlines ("The following request parameters are missing:" : e) +> badRequest $ unlines e hunk ./RqDataError.lhs 49 -The following request parameters are missing: -greeting -noun +Parameter not found: greeting +Parameter not found: noun hunk ./RqDataLimiting.lhs 7 -

Limiting lookup to QUERY_STRING or request body

+

Limiting lookup to QUERY_STRING or request body

addfile ./RqDataParsing.lhs hunk ./RqDataParsing.lhs 1 + + +

Handling non-String request parameters

+ +

The RqData module provides a variety of ways to work with values besides Strings.

+ +#include "RqDataRead.lhs" +#include "RqDataCheck.lhs" +#include "RqDataCheckOther.lhs" hunk ./Templates.lhs 25 +
BlazeHtml
+

The BlazeHtml library provides combinators for generating HTML 4 and HTML 5 in Haskell.

+

pros:

+ +

cons:

+ +
+ hunk ./Templates.lhs 54 +
  • Automatic escaping of String values
  • hunk ./Templates.lhs 65 -
  • Not ideal for having templates written by a non-programming web designer (although the xml syntax helps)
  • +
  • Not ideal for having templates written by a web designer who does not know Haskell (although the xml syntax helps)
  • +
  • No compile-time assurances that generated html/xml is valid (though it will be well-formed).
  • hunk ./Templates.lhs 69 -
    BlazeHtml
    hunk ./Templates.lhs 75 + +#include "HelloBlaze.lhs" +