From f1184a1f66079dec1bd4a7d9819eb00f1004211a Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 27 Jan 2010 13:19:27 +0200 Subject: [PATCH] Added ioTextToContent, fixed json/html order --- Yesod/Response.hs | 7 +++++++ Yesod/Template.hs | 10 ++-------- Yesod/Yesod.hs | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Yesod/Response.hs b/Yesod/Response.hs index a1106eee..d5e9abb9 100644 --- a/Yesod/Response.hs +++ b/Yesod/Response.hs @@ -24,6 +24,7 @@ module Yesod.Response , ChooseRep , HasReps (..) , defChooseRep + , ioTextToContent -- ** Convenience wrappers , staticRep -- * Response type @@ -90,6 +91,12 @@ instance ConvertSuccess XmlDoc Content where type ChooseRep = [ContentType] -> IO (ContentType, Content) +-- | It would be nice to simplify 'Content' to the point where this is +-- unnecesary. +ioTextToContent :: IO Text -> Content +ioTextToContent iotext = + Content $ \f a -> iotext >>= foldM f a . toChunks . cs + -- | Any type which can be converted to representations. class HasReps a where chooseRep :: a -> ChooseRep diff --git a/Yesod/Template.hs b/Yesod/Template.hs index 41b0aaa0..ee5f910e 100644 --- a/Yesod/Template.hs +++ b/Yesod/Template.hs @@ -21,8 +21,6 @@ import Web.Mime import Yesod.Response import Yesod.Yesod import Yesod.Handler -import Control.Monad (foldM) -import Data.ByteString.Lazy (toChunks) type Template = StringTemplate Text type TemplateGroup = STGroup Text @@ -44,8 +42,8 @@ template tn ho f = do Nothing -> failure $ NoSuchTemplate tn Just x -> return x return $ chooseRep - [ (TypeJson, cs $ unJsonDoc $ cs ho) - , (TypeHtml, tempToContent t ho f) + [ (TypeHtml, tempToContent t ho f) + , (TypeJson, cs $ unJsonDoc $ cs ho) ] newtype NoSuchTemplate = NoSuchTemplate String deriving (Show, Typeable) @@ -57,10 +55,6 @@ tempToContent :: Template -> Content tempToContent t ho f = ioTextToContent $ fmap render $ f ho t -ioTextToContent :: IO Text -> Content -ioTextToContent iotext = - Content $ \f a -> iotext >>= foldM f a . toChunks . cs - data TemplateFile = TemplateFile FilePath HtmlObject instance HasReps TemplateFile where chooseRep (TemplateFile fp (Mapping m)) _ = do diff --git a/Yesod/Yesod.hs b/Yesod/Yesod.hs index 33cd2aa0..729af6a9 100644 --- a/Yesod/Yesod.hs +++ b/Yesod/Yesod.hs @@ -75,8 +75,8 @@ applyLayoutJson :: Yesod y applyLayoutJson t b = do y <- getYesod return $ chooseRep - [ (TypeJson, cs $ unJsonDoc $ cs b) - , (TypeHtml, applyLayout y t $ cs b) + [ (TypeHtml, applyLayout y t $ cs b) + , (TypeJson, cs $ unJsonDoc $ cs b) ] getApproot :: YesodApproot y => Handler y Approot