Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add compiled splice for FlashNotice

  • Loading branch information...
commit 83864ed2317e4c0752f70e5d40985123c99042ec 1 parent 3750df5
@mightybyte mightybyte authored
Showing with 32 additions and 0 deletions.
  1. +32 −0 src/Snap/Extras/FlashNotice.hs
View
32 src/Snap/Extras/FlashNotice.hs
@@ -8,11 +8,14 @@ module Snap.Extras.FlashNotice
, flashSuccess
, flashError
, flashSplice
+ , flashCSplice
) where
-------------------------------------------------------------------------------
import Control.Monad
import Control.Monad.Trans
+import Data.Maybe
+import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Snap.Snaplet
@@ -20,6 +23,7 @@ import Snap.Snaplet.Heist
import Snap.Snaplet.Session
import Heist
import Heist.Interpreted
+import qualified Heist.Compiled as C
import Text.XmlHtml
-------------------------------------------------------------------------------
@@ -77,3 +81,31 @@ flashSplice session = do
callTemplateWithText "_flash"
[ ("type", typ') , ("message", msg') ]
+
+-------------------------------------------------------------------------------
+-- | A compiled splice for rendering a given flash notice dirctive.
+--
+-- Ex: <flash type='warning'/>
+-- Ex: <flash type='success'/>
+flashCSplice :: SnapletLens b SessionManager -> SnapletCSplice b
+flashCSplice session = do
+ n <- getParamNode
+ let typ = maybe "warning" id $ getAttribute "type" n
+ k = T.concat ["_", typ]
+ splice prom = do
+ flashTemplate <- C.withLocalSplices
+ [ ("type", return $ C.yieldPureText typ)
+ , ("message", return $ C.yieldRuntimeText $ liftM fromJust
+ $ C.getPromise prom) ]
+ [] (C.callTemplate "_flash")
+ return $ C.yieldRuntime $ do
+ msg <- C.getPromise prom
+ case msg of
+ Nothing -> return mempty
+ Just _ -> do
+ lift $ withTop session $
+ deleteFromSession k >> commitSession
+ C.codeGen flashTemplate
+ C.defer splice (lift $ withTop session $ getFromSession k)
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.