From 00635452b96eaa7cdef8239b5c02c59fd9b9e593 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Tue, 21 Jul 2015 11:07:52 -0400 Subject: [PATCH 1/4] Add IsString instance for WidgetT site m () --- yesod-core/Yesod/Core/Types.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/yesod-core/Yesod/Core/Types.hs b/yesod-core/Yesod/Core/Types.hs index 5fa5c3d3..f41bcbd0 100644 --- a/yesod-core/Yesod/Core/Types.hs +++ b/yesod-core/Yesod/Core/Types.hs @@ -48,7 +48,7 @@ import qualified Network.Wai.Parse as NWP import System.Log.FastLogger (LogStr, LoggerSet, toLogStr, pushLogStr) import qualified System.Random.MWC as MWC import Network.Wai.Logger (DateCacheGetter) -import Text.Blaze.Html (Html) +import Text.Blaze.Html (Html, toHtml) import Text.Hamlet (HtmlUrl) import Text.Julius (JavascriptUrl) import Web.Cookie (SetCookie) @@ -250,6 +250,10 @@ instance (a ~ (), Monad m) => Monoid (WidgetT site m a) where mempty = return () mappend x y = x >> y instance (a ~ (), Monad m) => Semigroup (WidgetT site m a) +instance Monad m => IsString (WidgetT site m ()) where + fromString = toWidget . toHtml . T.pack + where toWidget x = WidgetT $ const $ return $ ((), GWData (Body (const x)) + mempty mempty mempty mempty mempty mempty) type RY master = Route master -> [(Text, Text)] -> Text From 4e354c9e07fb099707d03e3cd48e1d4171996599 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Tue, 21 Jul 2015 11:37:09 -0400 Subject: [PATCH 2/4] Improve type inference for WidgetT IsString instance --- yesod-core/Yesod/Core/Types.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yesod-core/Yesod/Core/Types.hs b/yesod-core/Yesod/Core/Types.hs index f41bcbd0..d96f10bf 100644 --- a/yesod-core/Yesod/Core/Types.hs +++ b/yesod-core/Yesod/Core/Types.hs @@ -250,7 +250,7 @@ instance (a ~ (), Monad m) => Monoid (WidgetT site m a) where mempty = return () mappend x y = x >> y instance (a ~ (), Monad m) => Semigroup (WidgetT site m a) -instance Monad m => IsString (WidgetT site m ()) where +instance (Monad m, a ~ ()) => IsString (WidgetT site m a) where fromString = toWidget . toHtml . T.pack where toWidget x = WidgetT $ const $ return $ ((), GWData (Body (const x)) mempty mempty mempty mempty mempty mempty) From 66ed3148664cc045dad8c446f4384044d89339e5 Mon Sep 17 00:00:00 2001 From: Christopher Reichert Date: Tue, 21 Jul 2015 11:50:21 -0500 Subject: [PATCH 3/4] Document IsString instance for WidgetT. --- yesod-core/Yesod/Core/Types.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/yesod-core/Yesod/Core/Types.hs b/yesod-core/Yesod/Core/Types.hs index d96f10bf..331238c0 100644 --- a/yesod-core/Yesod/Core/Types.hs +++ b/yesod-core/Yesod/Core/Types.hs @@ -250,9 +250,16 @@ instance (a ~ (), Monad m) => Monoid (WidgetT site m a) where mempty = return () mappend x y = x >> y instance (a ~ (), Monad m) => Semigroup (WidgetT site m a) + +-- | Any type with an 'IsString' instance can be trivially +-- promoted to a widget. +-- +-- For example, in a yesod-scaffold site you could use: +-- +-- @getHomeR = do defaultLayout "Widget text"@ instance (Monad m, a ~ ()) => IsString (WidgetT site m a) where fromString = toWidget . toHtml . T.pack - where toWidget x = WidgetT $ const $ return $ ((), GWData (Body (const x)) + where toWidget x = WidgetT $ const $ return $ ((), GWData (Body (const x)) mempty mempty mempty mempty mempty mempty) type RY master = Route master -> [(Text, Text)] -> Text From a5a627db58ac41e37067f047889ca3b803285f7c Mon Sep 17 00:00:00 2001 From: Christopher Reichert Date: Tue, 21 Jul 2015 13:19:00 -0500 Subject: [PATCH 4/4] Clarify IsString instance for Widget only works with Strings. --- yesod-core/Yesod/Core/Types.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/yesod-core/Yesod/Core/Types.hs b/yesod-core/Yesod/Core/Types.hs index 331238c0..2c39f90d 100644 --- a/yesod-core/Yesod/Core/Types.hs +++ b/yesod-core/Yesod/Core/Types.hs @@ -251,8 +251,7 @@ instance (a ~ (), Monad m) => Monoid (WidgetT site m a) where mappend x y = x >> y instance (a ~ (), Monad m) => Semigroup (WidgetT site m a) --- | Any type with an 'IsString' instance can be trivially --- promoted to a widget. +-- | A 'String' can be trivially promoted to a widget. -- -- For example, in a yesod-scaffold site you could use: --