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.

4

1 回答 1

2

ToMessage String实例将响应类型设置为 text/plain 而不是 text/html。

您可以为本质上是原始实例的副本的新类型编写自己的String实例,但将响应类型设置为 text/html,或者使用库中的不同工具来更改响应类型。

此外,您可能应该注意到,在 404 错误上发送 200 HTTP 响应是令人困惑的。

于 2015-04-20T10:30:02.920 回答