Search code examples
haskellhappstack

Haskell Happstack, can I use a string with html tags as response?


    module Main where

    import Happstack.Lite
    import Text.Html

    main :: IO ()
    main = serve Nothing $ msum [
              nullDir >> ok homePage
            , notFound page404  
        ]

    homePage :: Response
    homePage = toResponse $ do
        p (toHtml "hello") +++ 
            strong (toHtml "BOLD")

    page404 :: Response
    page404 = toResponse "<strong>How do I parse the tag STRONG?</strong>"

Hi, I'm new to happstack. I'm wondering if there's a way I can just display a string with html tags as response instead of using a html template library?

In the above code, the <strong> tag in page404 is escaped, so I got "<strong>How do I pase the tag BOLD?</strong>" as response, while the one homePage is rendered as "How do I parse the tag BOLD".

Do I have to parse the string first? But wouldn't that be too slow if the html string is large?

Thanks in advance.


Solution

  • The ToMessage String instance sets the response type to text/plain rather than text/html.

    You can either write your own instance for a newtyped String that is essentially a copy of the original instance, but with the response type set to text/html, or use different facilities in the library to alter the response type.

    Also, you should probably note that sending a 200 HTTP response on a 404 error is confusing.