Question

I'm using HStringTemplate to render a very simple template using a data structure to fill in the "holes". The result of the template rendering is just a String I fed toResponse with.

Even though this rendered template is valid html happstack uses text/plain for Content-Type.

What is the reason for this? Shouldn't text/html be default since it is a webserver? Do I really need to use toResponseBS and set text/html by myself?

Here is the code that creates the ServerPart Response

data Person = Person                                                                                                                                                                                                
    { name :: String                                                                                                                                                                                                
    , age ::Int                                                                                                                                                                                                     
    } deriving (Data, Typeable)                                                                                                                                                                                     

buildTemplate :: Person -> String -> FilePath -> ServerPart Response                                                                                                                                                
buildTemplate fields name template = do                                                                                                                                                                             
    unrendered <- liftIO $ readFile template                                                                                                                                                                        
    ok $ toResponse $ renderTemplate name fields unrendered                                                                                                                                                         

renderTemplate :: String -> Person -> String -> String                                                                                                                                                              
renderTemplate name fields unrendered = toString rendered                                                                                                                                                           
    where rendered = setAttribute name fields $ newSTMP unrendered

And here is the output from the webserver:

Head

Connection:Keep-Alive
Content-Type:text/plain; charset=UTF-8
Date:Wed, 09 Jan 2013 14:51:27 GMT
Server:Happstack/7.1.1
Transfer-Encoding:chunked

Body

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Memlikweb</title>
    </head>
    <body>
        <h1>Hello, Richard!<h1>
        <p>Do you have 25 for me?</p>
    </body>
</html>
Was it helpful?

Solution

If you pass Text.Html to toResponse the content type will be text/html. You are passing a string, which toResponse takes to mean that the content type is plain text.

OTHER TIPS

The happstack-hstringtemplate package provides an instance for ToMessage StringTemplate which means if you import it and then use toResponse on the template without rendering it, it will do the right thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top