0
    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.

3
  • Are you sure the HTML is escaped? Looking at the ToMessage String instance, it looks like the response type is the only thing that's wrong. In that case you have a number of options: You can newtype String and create a separate instance, but with a different content type, or you can use different combinators. Commented Apr 20, 2015 at 1:16
  • Thanks for the reply. I successfully compiled and ran the above code. I don't think there's a type error. The tag is escaped, for example, in route http://localhost:8000/no-such-route Commented Apr 20, 2015 at 1:54
  • 1
    I didn't claim that there is. Only that you'll need to alter the response type, because the ToMessage String instance sets it to text/plain rather than text/html. Commented Apr 20, 2015 at 1:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.