Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix issue mentioned in

http://stackoverflow.com/questions/8297831/adding-onload-hooks-for-heist-templates-using-the-snap-web-framework
that prevents the use of Heist's onLoad hooks when using the Heist snaplet.
  • Loading branch information...
commit 29e9992af9b102df912baad5fb44c22895211b2f 1 parent b2df047
@mightybyte mightybyte authored
Showing with 43 additions and 13 deletions.
  1. +3 −1 src/Snap/Snaplet/Heist.hs
  2. +40 −12 src/Snap/Snaplet/HeistNoClass.hs
View
4 src/Snap/Snaplet/Heist.hs
@@ -14,6 +14,7 @@ module Snap.Snaplet.Heist
-- * Initializer Functions
-- $initializerSection
, heistInit
+ , heistInit'
, addTemplates
, addTemplatesAt
, modifyHeistTS
@@ -52,7 +53,8 @@ import Text.Templating.Heist
import Snap.Snaplet
import qualified Snap.Snaplet.HeistNoClass as Unclassed
-import Snap.Snaplet.HeistNoClass (Heist, heistInit, clearHeistCache)
+import Snap.Snaplet.HeistNoClass (Heist, heistInit
+ ,heistInit', clearHeistCache)
------------------------------------------------------------------------------
View
52 src/Snap/Snaplet/HeistNoClass.hs
@@ -7,6 +7,7 @@
module Snap.Snaplet.HeistNoClass
( Heist
, heistInit
+ , heistInit'
, clearHeistCache
, addTemplates
@@ -203,23 +204,50 @@ bindSnapletSplices l splices =
------------------------------------------------------------------------------
--- | The 'Initializer' for 'Heist'.
+-- | The 'Initializer' for 'Heist'. This function is a convenience wrapper
+-- around `heistInit'` that uses the default `emptyTemplateState` from Heist
+-- and sets up routes for all the templates.
heistInit :: FilePath
-> SnapletInit b (Heist b)
-heistInit templateDir =
+heistInit templateDir = do
makeSnaplet "heist" "" Nothing $ do
- (cacheFunc, cts) <- liftIO mkCacheTag
- let origTs = cacheFunc emptyTemplateState
- ts <- liftIO $ loadTemplates templateDir origTs >>=
- either error return
+ hs <- heistInitWorker templateDir emptyTemplateState
addRoutes [ ("", heistServe) ]
- printInfo $ T.pack $ unwords
- [ "...loaded"
- , (show $ length $ templateNames ts)
- , "templates"
- ]
+ return hs
- return $ Heist ts cts
+
+------------------------------------------------------------------------------
+-- | A lower level 'Initializer' for 'Heist'. This initializer requires you
+-- to specify the initial TemplateState. It also does not add any routes for
+-- templates, allowing you complete control over which templates get routed.
+heistInit' :: FilePath
+ -- ^ Path to templates
+ -> TemplateState (Handler b b)
+ -- ^ Initial TemplateState
+ -> SnapletInit b (Heist b)
+heistInit' templateDir initialTemplateState =
+ makeSnaplet "heist" "" Nothing $
+ heistInitWorker templateDir initialTemplateState
+
+
+------------------------------------------------------------------------------
+-- | Internal worker function used by variantsof heistInit. This is necessary
+-- because of the divide between SnapletInit and Initializer.
+heistInitWorker :: FilePath
+ -> TemplateState (Handler b b)
+ -> Initializer b v (Heist b)
+heistInitWorker templateDir initialTemplateState = do
+ (cacheFunc, cts) <- liftIO mkCacheTag
+ let origTs = cacheFunc initialTemplateState
+ ts <- liftIO $ loadTemplates templateDir origTs >>=
+ either error return
+ printInfo $ T.pack $ unwords
+ [ "...loaded"
+ , (show $ length $ templateNames ts)
+ , "templates"
+ ]
+
+ return $ Heist ts cts
addTemplates :: ByteString -> Initializer b (Heist b) ()
Please sign in to comment.
Something went wrong with that request. Please try again.