[web-devel] [yesod] how convert Content to String or Text

Michael Snoyman michael at snoyman.com
Wed Jul 6 22:02:31 CEST 2011


On Wed, Jul 6, 2011 at 12:18 PM, Dmitriy Nikitinskiy <nick at bel.ru> wrote:
> Hello All.
>
> I have following code:
>
> getMyRoute :: String -> Handler RepXml
> getMyRoute r = liftM RepXml $ hamletToContent  [hamlet|<node |]
>
> getOtherRoute :: Handler ()
> getOtherRoute = do
>   (RepXml mycontent) <- getMyRoute "something"
>   -- how write mycontent to file here?
>   -- let str = ????  or str <- ????
>   -- liftIO $ writeFile "myfile.xml" str
>   return ()
>
> How convert Yesod.Content.Content to String or Text?

Hi Dmitry,

I would try to convert the Content. Instead, I'd write a helper function:

    myXml :: String -> Hamlet MyRoute
    myXml r = [hamlet|<node|]

Then you could use myXml from both getMyRotue and getOtherRoute. In
order to render a Hamlet, you'll need to get a URL rendering
function[1]. So getOtherRoute would look something like:

   getOtherRoute = do
        renderUrl <- getUrlRenderParams
        let lbs = renderHamlet renderUrl $ myXml "something"
        liftIO $ Data.ByteString.Lazy.writeFile "myfile.xml" lbs

HTH,
Michael

[1] It's a function that converts a type-safe URL into a String.



More information about the web-devel mailing list