snap-0.11.2: Top-level package for the Snap Web Framework

Safe Haskell | None

Snap.Snaplet.Heist.Compiled

Contents

- Initializer Functions
- Handler Functions

Description

A module exporting only functions for using compiled templates. If you import the main Snap.Snaplet.Heist module, it's easy to accidentally use the interpreted render function even when you're using compiled Heist. Importing only this module will make it harder to make mistakes like that.

Synopsis

- data Heist b
- class HasHeist b where
  - heistLens :: SnapletLens (Snaplet b) (Heist b)
- type SnapletHeist b m a = HeistT (Handler b b) m a
- type SnapletCSplice b = SnapletHeist b IO (DList (Chunk (Handler b b)))
- heistInit :: FilePath -> SnapletInit b (Heist b)
- heistInit' :: FilePath -> HeistConfig (Handler b b) -> SnapletInit b (Heist b)
- addTemplates :: HasHeist b => Snaplet (Heist b) -> ByteString -> Initializer b v ()
- addTemplatesAt :: HasHeist b => Snaplet (Heist b) -> ByteString -> FilePath -> Initializer b v ()
- addConfig :: Snaplet (Heist b) -> HeistConfig (Handler b b) -> Initializer b v ()
- modifyHeistState :: HasHeist b => (HeistState (Handler b b) -> HeistState (Handler b b)) -> Initializer b v ()
- withHeistState :: HasHeist b => (HeistState (Handler b b) -> a) -> Handler b v a
- 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 ()
- clearHeistCache :: Heist b -> IO ()

# Documentation

data Heist b

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.

class HasHeist b where

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) }
     makeLenses ''App

     instance HasHeist App where heistLens = subSnaplet heist

     appInit = makeSnaplet "app" "" Nothing $ do
         h <- nestSnaplet "heist" heist $ heistInit "templates"
         addSplices myAppSplices
         return $ App h

Methods

heistLens :: SnapletLens (Snaplet b) (Heist b)

A lens to the Heist snaplet. The b parameter to Heist will typically be the base state of your application.

type SnapletHeist b m a = HeistT (Handler b b) m a

type SnapletCSplice b = SnapletHeist b IO (DList (Chunk (Handler b b)))

# Initializer Functions

heistInit

Arguments

:: FilePath | Path to templates
-> SnapletInit b (Heist b) |

The `Initializer` for `Heist`. This function is a convenience wrapper around `heistInit'` that uses defaultHeistState and sets up routes for all the templates.

heistInit'

Arguments

:: FilePath | Path to templates
-> HeistConfig (Handler b b) | Initial HeistConfig
-> SnapletInit b (Heist b) |

A lower level `Initializer` for `Heist`. This initializer requires you to specify the initial HeistConfig. It also does not add any routes for templates, allowing you complete control over which templates get routed.

addTemplates

Arguments

:: HasHeist b |
=> Snaplet (Heist b) |
-> ByteString | The url prefix for the template routes
-> Initializer b v () |

Adds templates to the Heist HeistState. 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

Arguments

:: HasHeist b |
=> Snaplet (Heist b) |
-> ByteString | URL prefix for template routes
-> FilePath | Path to templates
-> Initializer b v () |

Adds templates to the Heist HeistState, and lets you specify where they are found in the filesystem. Note that the path to the template directory is an absolute path. This allows you more flexibility in where your templates are located, but means that you have to explicitly call getSnapletFilePath if you want your snaplet to use templates within its normal directory structure.

addConfig :: Snaplet (Heist b) -> HeistConfig (Handler b b) -> Initializer b v ()

Adds more HeistConfig data using mappend with whatever is currently there. This is the preferred method for adding all four kinds of splices as well as new templates.

modifyHeistState

Arguments

:: HasHeist b |
=> (HeistState (Handler b b) -> HeistState (Handler b b)) | HeistState modifying function
-> Initializer b v () |

More general function allowing arbitrary HeistState modification.

withHeistState

Arguments

:: HasHeist b |
=> (HeistState (Handler b b) -> a) | HeistState function to run
-> Handler b v a |

Runs a function on with the Heist snaplet's `HeistState`.

# Handler Functions

render

Arguments

:: HasHeist b |
=> ByteString | Template name
-> Handler b v () |

Renders a compiled template as text/html. If the given template is not found, this returns `empty`.

renderAs

Arguments

:: HasHeist b |
=> ByteString | Content type to render with
-> ByteString | Template name
-> Handler b v () |

Renders a compiled template as the given content type. If the given template is not found, this returns `empty`.

heistServe :: HasHeist b => Handler b v ()

A handler that serves all the templates (similar to `serveDirectory`). If the template specified in the request path is not found, it returns `empty`. Also, this function does not serve any templates beginning with an underscore. This gives you a way to prevent some templates from being served. For example, you might have a template that contains only the navbar of your pages, and you probably wouldn't want that template to be visible to the user as a standalone template. So if you put it in a file called "_nav.tpl", this function won't serve it.

heistServeSingle

Arguments

:: HasHeist b |
=> ByteString | Template name
-> Handler b v () |

Handler for serving a single template (similar to `fileServeSingle`). If the given template is not found, this throws an error.

clearHeistCache :: Heist b -> IO ()

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.