Permalink
Browse files

Don't render templates if the name begins with underscore

  • Loading branch information...
1 parent b50d6c6 commit b107117910cbe1af3bf055d68806a2047f96dea4 @mightybyte mightybyte committed Sep 11, 2012
Showing with 17 additions and 5 deletions.
  1. +6 −3 src/Snap/Snaplet/Heist.hs
  2. +11 −2 src/Snap/Snaplet/HeistNoClass.hs
@@ -200,7 +200,11 @@ cRenderAs ct t = withTop' heistLens (Unclassed.cRenderAs ct t)
------------------------------------------------------------------------------
-- | Analogous to 'fileServe'. If the template specified in the request path
--- is not found, it returns 'empty'.
+-- is not found, it returns 'empty'. Also, this function does not serve any
+-- templates beginning with an underscore. This gives you a way to prevent
+-- some templates from being served. For example, you might have a template
+-- that contains only the navbar of your pages, and you wouldn't want that
+-- template to be visible to the user as a standalone template.
heistServe :: HasHeist b => Handler b v ()
heistServe = withTop' heistLens Unclassed.heistServe
@@ -216,8 +220,7 @@ heistServeSingle t = withTop' heistLens (Unclassed.heistServeSingle t)
------------------------------------------------------------------------------
--- | Analogous to 'fileServe'. If the template specified in the request path
--- is not found, it returns 'empty'.
+-- | A compiled version of 'heistServe'.
cHeistServe :: HasHeist b => Handler b v ()
cHeistServe = withTop' heistLens Unclassed.cHeistServe
@@ -470,9 +470,18 @@ cRenderAs ct t = cRenderHelper (Just ct) t
------------------------------------------------------------------------------
+serveURI :: Handler b (Heist b) ByteString
+serveURI = do
+ p <- getSafePath
+ -- Allows users to prefix template filenames with an underscore to prevent
+ -- the template from being served.
+ if head p == '_' then pass else return $ B.pack p
+
+
+------------------------------------------------------------------------------
heistServe :: Handler b (Heist b) ()
heistServe =
- ifTop (render "index") <|> (render . B.pack =<< getSafePath)
+ ifTop (render "index") <|> (render =<< serveURI)
------------------------------------------------------------------------------
@@ -485,7 +494,7 @@ heistServeSingle t =
------------------------------------------------------------------------------
cHeistServe :: Handler b (Heist b) ()
cHeistServe =
- ifTop (cRender "index") <|> (cRender . B.pack =<< getSafePath)
+ ifTop (cRender "index") <|> (cRender =<< serveURI)
------------------------------------------------------------------------------

0 comments on commit b107117

Please sign in to comment.