Skip to content
Browse files

Simplified HasHeist down to just one type parameter.

  • Loading branch information...
1 parent 335ebb2 commit d109d46fb1f84d166ab3de60967d26c35f442f06 @mightybyte mightybyte committed Aug 3, 2011
View
2 examples/App.hs
@@ -26,7 +26,7 @@ type AppHandler = Handler App App
mkLabels [''App]
-instance HasHeist App App where
+instance HasHeist App where
heistLens = subSnaplet heist
helloHandler :: AppHandler ()
View
40 src/Snap/Snaplet/Heist.hs
@@ -52,7 +52,6 @@ module Snap.Snaplet.Heist
) where
import Prelude hiding (id, (.))
-import Control.Category
import Data.ByteString (ByteString)
import Data.Record.Label
import Data.Text (Text)
@@ -73,25 +72,16 @@ import Snap.Snaplet.HeistNoClass (Heist, heistInit, clearHeistCache)
-- > data App = App { _heist :: Snaplet (Heist App) }
-- > mkLabels [''App]
-- >
--- > instance HasHeist App App where heistLens = subSnaplet heist
+-- > instance HasHeist App where heistLens = subSnaplet heist
-- >
-- > appInit = makeSnaplet "app" "" Nothing $ do
-- > h <- nestSnaplet "heist" $ heistInit "templates"
-- > addSplices myAppSplices
-- > return $ App h
-class HasHeist b v where
- -- | A lens from v to the Heist snaplet. The b parameter to Heist will
- -- typically be the base state of your application. Most of the time
- -- 'b' and 'v' will be the same.
- heistLens :: Snaplet v :-> Snaplet (Heist b)
-
-
-------------------------------------------------------------------------------
--- | This default instance allows you to avoid writing a HasHeist instance and
--- instead access the heist snaplet via the 'with'' or 'withTop''
--- functions. This might be preferrable in situations where Heist usage is
--- very simple.
-instance HasHeist b (Heist b) where heistLens = id
+class HasHeist b where
+ -- | A lens to the Heist snaplet. The b parameter to Heist will
+ -- typically be the base state of your application.
+ heistLens :: Snaplet b :-> Snaplet (Heist b)
-- $initializerSection
@@ -103,20 +93,20 @@ instance HasHeist b (Heist b) where heistLens = id
-- | Adds templates to the Heist TemplateState. Other snaplets should use
-- this function to add their own templates. The templates are automatically
-- read from the templates directory in the current snaplet's filesystem root.
-addTemplates :: HasHeist b b => ByteString -> Initializer b v ()
+addTemplates :: HasHeist b => ByteString -> Initializer b v ()
addTemplates pfx = withTop' heistLens (Unclassed.addTemplates pfx)
------------------------------------------------------------------------------
-- | Adds templates to the Heist TemplateState, and lets you specify where
-- they are fonud in the filesystem.
-addTemplatesAt :: HasHeist b b => ByteString -> FilePath -> Initializer b v ()
+addTemplatesAt :: HasHeist b => ByteString -> FilePath -> Initializer b v ()
addTemplatesAt pfx p = withTop' heistLens (Unclassed.addTemplatesAt pfx p)
------------------------------------------------------------------------------
-- | Allows snaplets to add splices.
-addSplices :: (HasHeist b b)
+addSplices :: (HasHeist b)
=> [(Text, Unclassed.SnapletSplice b v)] -> Initializer b v ()
addSplices = Unclassed.addSplices' heistLens
@@ -129,35 +119,35 @@ addSplices = Unclassed.addSplices' heistLens
------------------------------------------------------------------------------
-- | Renders a template as text\/html. If the given template is not found,
-- this returns 'empty'.
-render :: HasHeist b b => ByteString -> Handler b v ()
+render :: HasHeist b => ByteString -> Handler b v ()
render t = withTop' heistLens (Unclassed.render t)
------------------------------------------------------------------------------
-- | Renders a template as the given content type. If the given template
-- is not found, this returns 'empty'.
-renderAs :: HasHeist b b => ByteString -> ByteString -> Handler b v ()
+renderAs :: HasHeist b => ByteString -> ByteString -> Handler b v ()
renderAs ct t = withTop' heistLens (Unclassed.renderAs ct t)
------------------------------------------------------------------------------
-- | Analogous to 'fileServe'. If the template specified in the request path
-- is not found, it returns 'empty'.
-heistServe :: HasHeist b b => Handler b v ()
+heistServe :: HasHeist b => Handler b v ()
heistServe = withTop' heistLens Unclassed.heistServe
------------------------------------------------------------------------------
-- | Analogous to 'fileServeSingle'. If the given template is not found,
-- this throws an error.
-heistServeSingle :: HasHeist b b => ByteString -> Handler b v ()
+heistServeSingle :: HasHeist b => ByteString -> Handler b v ()
heistServeSingle t = withTop' heistLens (Unclassed.heistServeSingle t)
------------------------------------------------------------------------------
-- | Renders a template with a given set of splices. This is syntax sugar for
-- a common combination of heistLocal, bindSplices, and render.
-renderWithSplices :: HasHeist b b
+renderWithSplices :: HasHeist b
=> ByteString
-> [(Text, Unclassed.SnapletSplice b v)]
-> Handler b v ()
@@ -167,7 +157,7 @@ renderWithSplices = Unclassed.renderWithSplices' heistLens
------------------------------------------------------------------------------
-- | Runs an action with additional splices bound into the Heist
-- 'TemplateState'.
-withSplices :: HasHeist b b
+withSplices :: HasHeist b
=> [(Text, Unclassed.SnapletSplice b v)]
-> Handler b v a
-> Handler b v a
@@ -180,7 +170,7 @@ withSplices = Unclassed.withSplices' heistLens
-- action. To do that you would do:
--
-- > heistLocal (bindSplices mySplices) handlerThatNeedsSplices
-heistLocal :: HasHeist b b
+heistLocal :: HasHeist b
=> (TemplateState (Handler b b) -> TemplateState (Handler b b))
-> Handler b v a
-> Handler b v a
View
5 test/suite/NestTest.hs
@@ -31,10 +31,7 @@ data FooSnaplet = FooSnaplet
mkLabels [''FooSnaplet]
---instance HasHeist b (FooSnaplet) where
--- heistLens = subSnaplet fooHeist
-
-instance HasHeist FooSnaplet FooSnaplet where
+instance HasHeist FooSnaplet where
heistLens = subSnaplet fooHeist
fooInit :: SnapletInit FooSnaplet FooSnaplet
View
2 test/suite/Snap/Snaplet/App.hs
@@ -36,7 +36,7 @@ data App = App
mkLabels [''App]
-instance HasHeist App App where heistLens = subSnaplet heist
+instance HasHeist App where heistLens = subSnaplet heist
routeWithSplice :: Handler App App ()
routeWithSplice = do
View
2 test/suite/Snap/Snaplet/BarSnaplet.hs
@@ -17,7 +17,7 @@ import Text.Templating.Heist
data BarSnaplet = BarSnaplet { barField :: String }
-barInit :: HasHeist b b
+barInit :: HasHeist b
=> SnapletInit b BarSnaplet
barInit = makeSnaplet "barsnaplet" "An example snaplet called bar." Nothing $ do
config <- getSnapletConfig
View
2 test/suite/Snap/Snaplet/FooSnaplet.hs
@@ -16,7 +16,7 @@ import Text.Templating.Heist
data FooSnaplet = FooSnaplet { fooField :: String }
-fooInit :: HasHeist b b => SnapletInit b FooSnaplet
+fooInit :: HasHeist b => SnapletInit b FooSnaplet
fooInit = makeSnaplet "foosnaplet" "A demonstration snaplet called foo." Nothing $ do
config <- getSnapletConfig
addTemplates "foo"

0 comments on commit d109d46

Please sign in to comment.
Something went wrong with that request. Please try again.