The Heist snaplet makes it easy to add Heist to your application and use it in other snaplets.
- data Heist b
- class HasHeist b where
- heistInit :: FilePath -> SnapletInit b (Heist b)
- addTemplates :: HasHeist b => ByteString -> Initializer b v ()
- addTemplatesAt :: HasHeist b => ByteString -> FilePath -> Initializer b v ()
- modifyHeistTS :: HasHeist b => (TemplateState (Handler b b) -> TemplateState (Handler b b)) -> Initializer b v ()
- withHeistTS :: HasHeist b => (TemplateState (Handler b b) -> a) -> Handler b v a
- addSplices :: HasHeist b => [(Text, SnapletSplice b v)] -> Initializer b v ()
- render :: HasHeist b => ByteString -> Handler b v ()
- renderAs :: HasHeist b => ByteString -> ByteString -> Handler b v ()
- heistServe :: HasHeist b => Handler b v ()
- heistServeSingle :: HasHeist b => ByteString -> Handler b v ()
- heistLocal :: HasHeist b => (TemplateState (Handler b b) -> TemplateState (Handler b b)) -> Handler b v a -> Handler b v a
- withSplices :: HasHeist b => [(Text, SnapletSplice b v)] -> Handler b v a -> Handler b v a
- renderWithSplices :: HasHeist b => ByteString -> [(Text, SnapletSplice b v)] -> Handler b v ()
- data SnapletHeist b v a
- type SnapletSplice b v = SnapletHeist b v Template
- liftHeist :: HeistT (Handler b b) a -> SnapletHeist b v a
- liftHandler :: Handler b v a -> SnapletHeist b v a
- liftAppHandler :: Handler b b a -> SnapletHeist b v a
- liftWith :: Lens (Snaplet b) (Snaplet v') -> Handler b v' a -> SnapletHeist b v a
- bindSnapletSplices :: Lens (Snaplet b) (Snaplet v) -> [(Text, SnapletSplice b v)] -> TemplateState (Handler b b) -> TemplateState (Handler b b)
- clearHeistCache :: Heist b -> IO ()
Heist and its type class
The state for the Heist snaplet. To use the Heist snaplet in your app
include this in your application state and use heistInit
to initialize
it. The type parameter b will typically be the base state type for your
application.
A single snaplet should never need more than one instance of Heist as a subsnaplet. This type class allows you to make it easy for other snaplets to get the lens that identifies the heist snaplet. Here's an example of how the heist snaplet might be declared:
data App = App { _heist :: Snaplet (Heist App) } mkLabels [''App] instance HasHeist App where heistLens = subSnaplet heist appInit = makeSnaplet "app" "" Nothing $ do h <- nestSnaplet "heist" $ heistInit "templates" addSplices myAppSplices return $ App h
Initializer Functions
This section contains functions for use in setting up your Heist state during initialization.
heistInit :: FilePath -> SnapletInit b (Heist b)Source
The Initializer
for Heist
.
addTemplates :: HasHeist b => ByteString -> Initializer b v ()Source
Adds templates to the Heist TemplateState. Other snaplets should use this function to add their own templates. The templates are automatically read from the templates directory in the current snaplet's filesystem root.
addTemplatesAt :: HasHeist b => ByteString -> FilePath -> Initializer b v ()Source
Adds templates to the Heist TemplateState, and lets you specify where they are found in the filesystem.
modifyHeistTS :: HasHeist b => (TemplateState (Handler b b) -> TemplateState (Handler b b)) -> Initializer b v ()Source
More general function allowing arbitrary TemplateState modification. Without this function you wouldn't be able to bind more complicated splices like the cache tag.
withHeistTS :: HasHeist b => (TemplateState (Handler b b) -> a) -> Handler b v aSource
Runs a function on with the Heist snaplet's TemplateState
.
addSplices :: HasHeist b => [(Text, SnapletSplice b v)] -> Initializer b v ()Source
Allows snaplets to add splices.
Handler Functions
This section contains functions in the Handler
monad that you'll use in
processing requests.
render :: HasHeist b => ByteString -> Handler b v ()Source
Renders a template as text/html. If the given template is not found,
this returns empty
.
renderAs :: HasHeist b => ByteString -> ByteString -> Handler b v ()Source
Renders a template as the given content type. If the given template
is not found, this returns empty
.
heistServe :: HasHeist b => Handler b v ()Source
Analogous to fileServe
. If the template specified in the request path
is not found, it returns empty
.
heistServeSingle :: HasHeist b => ByteString -> Handler b v ()Source
Analogous to fileServeSingle
. If the given template is not found,
this throws an error.
heistLocal :: HasHeist b => (TemplateState (Handler b b) -> TemplateState (Handler b b)) -> Handler b v a -> Handler b v aSource
Runs a handler with a modified TemplateState
. You might want to use
this if you had a set of splices which were customised for a specific
action. To do that you would do:
heistLocal (bindSplices mySplices) handlerThatNeedsSplices
withSplices :: HasHeist b => [(Text, SnapletSplice b v)] -> Handler b v a -> Handler b v aSource
Runs an action with additional splices bound into the Heist
TemplateState
.
renderWithSplices :: HasHeist b => ByteString -> [(Text, SnapletSplice b v)] -> Handler b v ()Source
Renders a template with a given set of splices. This is syntax sugar for a common combination of heistLocal, bindSplices, and render.
Writing Splices
As can be seen in the type signature of heistLocal, the internal
TemplateState used by the heist snaplet is parameterized by (Handler b b).
The reasons for this are beyond the scope of this discussion, but the
result is that lift
inside a splice only works with Handler b b
actions. When you're writing your own snaplets you obviously would rather
work with Handler b v
so your local snaplet's state is available. We
provide the SnapletHeist monad to make this possible. The general rule is
that when you're using Snaplets and Heist, use SnapletHeist instead of
HeistT (previously called TemplateMonad) and use SnapletSplice instead of
Splice.
data SnapletHeist b v a Source
Monad for working with Heist's API from within a snaplet.
MonadSnaplet SnapletHeist | MonadSnaplet instance gives us access to the snaplet infrastructure. |
MonadState v (SnapletHeist b v) | |
Monad (SnapletHeist b v) | |
Functor (SnapletHeist b v) | |
MonadPlus (SnapletHeist b v) | |
Applicative (SnapletHeist b v) | |
MonadCatchIO (SnapletHeist b v) | |
Alternative (SnapletHeist b v) | |
MonadIO (SnapletHeist b v) | |
MonadSnap (SnapletHeist b v) | |
MonadReader (Lens (Snaplet b) (Snaplet v)) (SnapletHeist b v) |
type SnapletSplice b v = SnapletHeist b v TemplateSource
Type alias for convenience.
liftHeist :: HeistT (Handler b b) a -> SnapletHeist b v aSource
Lifts a HeistT action into SnapletHeist. Use this with all the functions from the Heist API.
liftHandler :: Handler b v a -> SnapletHeist b v aSource
Lifts a Handler into SnapletHeist.
liftAppHandler :: Handler b b a -> SnapletHeist b v aSource
Lifts a (Handler b b) into SnapletHeist.
liftWith :: Lens (Snaplet b) (Snaplet v') -> Handler b v' a -> SnapletHeist b v aSource
Common idiom for the combination of liftHandler and withTop.
bindSnapletSplices :: Lens (Snaplet b) (Snaplet v) -> [(Text, SnapletSplice b v)] -> TemplateState (Handler b b) -> TemplateState (Handler b b)Source
SnapletSplices version of bindSplices.
clearHeistCache :: Heist b -> IO ()Source
Clears data stored by the cache tag. The cache tag automatically reloads its data when the specified TTL expires, but sometimes you may want to trigger a manual reload. This function lets you do that.