Skip to content

Commit

Permalink
Add ifElse splice functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mightybyte committed Jun 16, 2015
1 parent 90ca344 commit c1050af
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Heist/Splices.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Heist.Splices
( ifISplice
, ifCSplice
, ifElseISplice
, ifElseCSplice
, module Heist.Splices.Apply
, module Heist.Splices.Bind
, module Heist.Splices.Cache
Expand All @@ -19,6 +21,7 @@ import Heist.Splices.Html
import Heist.Splices.Ignore
import Heist.Splices.Markdown
import Heist.Internal.Types.HeistState
import qualified Text.XmlHtml as X

------------------------------------------------------------------------------
-- | Run the splice contents if given condition is True, make splice disappear
Expand Down Expand Up @@ -47,3 +50,23 @@ ifCSplice predicate runtime = do
else
return mempty


------------------------------------------------------------------------------
-- | Implements an if/then/else conditional splice. It splits its children
-- around the <else/> element to get the markup to be used for the two cases.
ifElseISplice :: Monad m => Bool -> I.Splice m
ifElseISplice cond = getParamNode >>= (rewrite . X.childNodes)
where
rewrite nodes =
let (ns, ns') = break (\n -> X.tagName n==Just "else") nodes
in I.runNodeList $ if cond then ns else (drop 1 ns')


------------------------------------------------------------------------------
-- | Implements an if/then/else conditional splice. It splits its children
-- around the <else/> element to get the markup to be used for the two cases.
ifElseCSplice :: Monad m => Bool -> C.Splice m
ifElseCSplice cond = getParamNode >>= (rewrite . X.childNodes)
where rewrite nodes =
let (ns, ns') = break (\n -> X.tagName n==Just "else") nodes
in C.runNodeList $ if cond then ns else (drop 1 ns')

0 comments on commit c1050af

Please sign in to comment.