haskellhaskell-snap-frameworkhamlet

Working with Hamletfile's Q Exp return value


Given this last line in my Haskell learning project:

writeText $ TL.toStrict (renderHtml $(hamletFile "fileList.hamlet")) 

I would like to convert the output from hamletFile's signature of:

FilePath -> Q Exp

into the Html type expected by the renderHtml function.

*Big chance I'm completely wrong in my above assertions of what I'm currently doing!


Solution

  • You're pretty close to getting this working. As Bartek and MathematicalOrchid are both saying, hamletFile "fileList.hamlet" has type Q Exp, which means "GHC can run this to generate an expression." By wrapping it in $(...), you're saying, "GHC, please generate an expression." The next question you need to ask it: what's the type of this generated expression?

    You can get more information in the shakespeare chapter of the Yesod book, but the expression will have type:

    (url -> [(Text, Text)] -> Text) -> Html
    

    Which basically means "tell me how to turn a URL and some query string parameters into text, and I'll give you some HTML." If you're not using any type-safe URLs in your template, you can simply provide undefined (or switch to using shamletFile, see the chapter for more information).

    Once you provide the URL rendering function, you can use renderHtml to get your lazy strict and continue from there.