Permalink
Browse files

Added fancyLoopSplice

  • Loading branch information...
1 parent c79616f commit 2e589257a7f36d4570ea7e16f8734220eb664a20 @mightybyte mightybyte committed Feb 18, 2014
Showing with 32 additions and 0 deletions.
  1. +32 −0 src/Snap/Extras/SpliceUtils/Compiled.hs
@@ -4,6 +4,7 @@ module Snap.Extras.SpliceUtils.Compiled where
-------------------------------------------------------------------------------
import Blaze.ByteString.Builder.ByteString
+import Control.Monad
import Control.Monad.Trans
import Data.Monoid
import qualified Data.Text as T
@@ -12,6 +13,7 @@ import Snap.Core
import qualified Snap.Extras.SpliceUtils.Interpreted as I
import Heist
import Heist.Compiled
+import Heist.Compiled.LowLevel
import Text.XmlHtml
-------------------------------------------------------------------------------
@@ -64,3 +66,33 @@ scriptsSplice :: MonadIO m
scriptsSplice d prefix = runNodeList =<< I.scriptsSplice d prefix
+------------------------------------------------------------------------------
+-- | Very similar to manyWithSplices, but this splice binds two additional
+-- splices \"whenNonzero\" and \"whenMultiple\". The former only shows its
+-- children when the list has 1 or more elements. The latter only shows its
+-- children when the list has more than 1 element.
+fancyLoopSplice :: Monad n
+ => Splices (RuntimeSplice n a -> Splice n)
+ -> RuntimeSplice n [a]
+ -> Splice n
+fancyLoopSplice splices action = do
+ p <- newEmptyPromise
+ q <- newEmptyPromise
+ let splices' = do
+ mapS ($ getPromise q) splices
+ "whenNonzero" ## checkPred (> 0) p
+ "whenMultiple" ## checkPred (> 1) p
+
+ chunks <- withLocalSplices splices' noSplices runChildren
+ return $ yieldRuntime $ do
+ items <- action
+ putPromise p $ length items
+ res <- forM items $ \item -> putPromise q item >> codeGen chunks
+ return $ mconcat res
+ where
+ checkPred predicate p = do
+ chunks <- runChildren
+ return $ yieldRuntime $ do
+ len <- getPromise p
+ if predicate len then codeGen chunks else return mempty
+

0 comments on commit 2e58925

Please sign in to comment.