In yesod (haskell), how can I load a simple html-formatted file (and not a village) as a widget?

How to load a simple HTML-formatted file (and not a tree file) as a widget? In other words, I'm looking for the equivalent of html:

toWidget $(whamletFile "test.hamlet")
+4
source share
1 answer

To do this, you use a sendFilehandler in your function ( see its definition )

The first argument is the Mime type, and the second is the file path.

For example, you can encode something like:

getMyFileR :: Handler ()
getMyFileR = sendFile "text/html" "myfile.html"

Here is another example. Let's say I have the following model:

Resource
    filename    FilePath
    mimetype    ContentType

    deriving    Typeable

A handler might look like this:

resourceDirectory :: FilePath
resourceDirectory = "resource"

getResourceGetR :: ResourceId -> Handler ()
getResourceGetR resourceId = do
    resource <- runDB $ get404 resourceId

    sendFile (resourceMimetype resource)
             (resourceDirectory </> unpack (resourceFilename resource))

Edit 2015-06-05

sendFileworks at a low level, addScriptor $(widgetFile …)works at a higher level.

$(widgetFile …) TemplateHaskell Hamlet/Cassius/Lucius/Julius Haskell . [hamlet|…|].

addScript Route, sendFile FilePath. , addScript . sendFile .

Html Hamlet:

addScript Html : addScript script, . Html .

+6

All Articles