Skip to content

Instantly share code, notes, and snippets.

@mightybyte
Created March 11, 2011 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mightybyte/866161 to your computer and use it in GitHub Desktop.
Save mightybyte/866161 to your computer and use it in GitHub Desktop.
Useful Heist Splices
------------------------------------------------------------------------------
-- | Renders the child nodes only if the request comes from an authenticated
-- user.
ifLoggedIn :: Splice Application
ifLoggedIn = do
node <- getParamNode
res <- lift $ requireUser (return []) (return $ childNodes node)
return res
------------------------------------------------------------------------------
-- | Renders the child nodes only if the request comes from a user that is not
-- logged in.
ifGuest :: Splice Application
ifGuest = do
node <- getParamNode
res <- lift $ requireUser (return $ childNodes node) (return [])
return res
------------------------------------------------------------------------------
-- | Returns the current username as a text node.
currentUser :: Splice Application
currentUser = do
mu <- lift currentAuthUser
return $ maybe [] (\u -> [TextNode $ uUsername u]) $ (cast' . Doc . snd) =<< mu
------------------------------------------------------------------------------
-- | Prevents the whole page from rendering if the user isn't logged in.
requireAuth :: Splice Application
requireAuth = do
lift $ requireUser pass (return [])
appSplices :: [(T.Text, Splice Application)]
appSplices =
[("ifLoggedIn", ifLoggedIn)
,("ifGuest", ifGuest)
,("requireAuth", requireAuth)
,("currentUser", currentUser)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment