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

Dmitriy Nikitinskiy nick at bel.ru
Thu Jul 7 07:15:29 CEST 2011


07.07.2011 00:02, Michael Snoyman пишет:
> 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.
>
Thanks Michael, it's helped me a lot.

Best regards,
Dmitriy.



More information about the web-devel mailing list