module Text.Templating.Heist.Splices.Apply where
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Text.XmlHtml as X
import Text.Templating.Heist.Internal
import Text.Templating.Heist.Types
applyTag :: Text
applyTag = "apply"
applyAttr :: Text
applyAttr = "template"
applyImpl :: Monad m => Splice m
applyImpl = do
node <- getParamNode
case X.getAttribute applyAttr node of
Nothing -> return []
Just attr -> do
st <- getTS
maybe (return [])
(\(t,ctx) -> do
addDoctype $ maybeToList $ X.docType t
processedChildren <- runNodeList $ X.childNodes node
modifyTS (bindSplice "content" $ return processedChildren)
setContext ctx
result <- runNodeList $ X.docContent t
restoreTS st
return result)
(lookupTemplate (T.encodeUtf8 attr)
(st {_curContext = nextCtx attr st}))
where nextCtx name st
| T.isPrefixOf "/" name = []
| otherwise = _curContext st