Skip to content
This repository
Browse code

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
Doug Beardsley authored November 29, 2011
4  src/Snap/Snaplet/Heist.hs
@@ -14,6 +14,7 @@ module Snap.Snaplet.Heist
14 14
   -- * Initializer Functions
15 15
   -- $initializerSection
16 16
   , heistInit
  17
+  , heistInit'
17 18
   , addTemplates
18 19
   , addTemplatesAt
19 20
   , modifyHeistTS
@@ -52,7 +53,8 @@ import           Text.Templating.Heist
52 53
 import           Snap.Snaplet
53 54
 
54 55
 import qualified Snap.Snaplet.HeistNoClass as Unclassed
55  
-import           Snap.Snaplet.HeistNoClass (Heist, heistInit, clearHeistCache)
  56
+import           Snap.Snaplet.HeistNoClass (Heist, heistInit
  57
+                                           ,heistInit', clearHeistCache)
56 58
 
57 59
 
58 60
 ------------------------------------------------------------------------------
52  src/Snap/Snaplet/HeistNoClass.hs
@@ -7,6 +7,7 @@
7 7
 module Snap.Snaplet.HeistNoClass
8 8
   ( Heist
9 9
   , heistInit
  10
+  , heistInit'
10 11
   , clearHeistCache
11 12
 
12 13
   , addTemplates
@@ -203,23 +204,50 @@ bindSnapletSplices l splices =
203 204
 
204 205
 
205 206
 ------------------------------------------------------------------------------
206  
--- | The 'Initializer' for 'Heist'.
  207
+-- | The 'Initializer' for 'Heist'.  This function is a convenience wrapper
  208
+-- around `heistInit'` that uses the default `emptyTemplateState` from Heist
  209
+-- and sets up routes for all the templates.
207 210
 heistInit :: FilePath
208 211
           -> SnapletInit b (Heist b)
209  
-heistInit templateDir =
  212
+heistInit templateDir = do
210 213
     makeSnaplet "heist" "" Nothing $ do
211  
-        (cacheFunc, cts) <- liftIO mkCacheTag
212  
-        let origTs = cacheFunc emptyTemplateState
213  
-        ts <- liftIO $ loadTemplates templateDir origTs >>=
214  
-                       either error return
  214
+        hs <- heistInitWorker templateDir emptyTemplateState
215 215
         addRoutes [ ("", heistServe) ]
216  
-        printInfo $ T.pack $ unwords
217  
-            [ "...loaded"
218  
-            , (show $ length $ templateNames ts)
219  
-            , "templates"
220  
-            ]
  216
+        return hs
221 217
 
222  
-        return $ Heist ts cts
  218
+
  219
+------------------------------------------------------------------------------
  220
+-- | A lower level 'Initializer' for 'Heist'.  This initializer requires you
  221
+-- to specify the initial TemplateState.  It also does not add any routes for
  222
+-- templates, allowing you complete control over which templates get routed.
  223
+heistInit' :: FilePath
  224
+           -- ^ Path to templates
  225
+           -> TemplateState (Handler b b)
  226
+           -- ^ Initial TemplateState
  227
+           -> SnapletInit b (Heist b)
  228
+heistInit' templateDir initialTemplateState =
  229
+    makeSnaplet "heist" "" Nothing $
  230
+        heistInitWorker templateDir initialTemplateState
  231
+
  232
+
  233
+------------------------------------------------------------------------------
  234
+-- | Internal worker function used by variantsof heistInit.  This is necessary
  235
+-- because of the divide between SnapletInit and Initializer.
  236
+heistInitWorker :: FilePath
  237
+                -> TemplateState (Handler b b)
  238
+                -> Initializer b v (Heist b)
  239
+heistInitWorker templateDir initialTemplateState = do
  240
+    (cacheFunc, cts) <- liftIO mkCacheTag
  241
+    let origTs = cacheFunc initialTemplateState
  242
+    ts <- liftIO $ loadTemplates templateDir origTs >>=
  243
+                   either error return
  244
+    printInfo $ T.pack $ unwords
  245
+        [ "...loaded"
  246
+        , (show $ length $ templateNames ts)
  247
+        , "templates"
  248
+        ]
  249
+
  250
+    return $ Heist ts cts
223 251
 
224 252
 
225 253
 addTemplates :: ByteString -> Initializer b (Heist b) ()

0 notes on commit 29e9992

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