Safe Haskell | None |
---|
Hastache templating for Scotty
{-# LANGUAGE OverloadedStrings #-} module Main where import Text.Hastache import Web.Scotty.Trans as S import Web.Scotty.Hastache main :: IO () main = scottyH 3000 $ do setTemplatesDir "templates" -- ^ Setting up the director with templates get "/:word" $ do beam <- param "word" setH "action" $ MuVariable (beam :: String) -- ^ "action" will be binded to the contents of 'beam' hastache "greet.html"
Given the following template:
<h1>Scotty, {{action}} me up!</h1>
Upon the GET /beam
the result will be:
<h1>Scotty, beam me up!</h1>
- scottyH :: Port -> ScottyH () -> IO ()
- scottyHOpts :: Options -> ScottyH () -> IO ()
- type ScottyH = ScottyT HState
- type ActionH = ActionT HState
- setHastacheConfig :: MuConfig IO -> ScottyH ()
- modifyHastacheConfig :: (MuConfig IO -> MuConfig IO) -> ScottyH ()
- setTemplatesDir :: FilePath -> ScottyH ()
- setTemplateFileExt :: String -> ScottyH ()
- hastache :: FilePath -> ActionT HState ()
- setH :: String -> MuType IO -> ActionT HState ()
- type HState = StateT (MuConfig IO, Map String (MuType IO)) IO
- mkHStateRunners :: MuConfig IO -> IO (forall a. HState a -> IO a, HState Response -> IO Response)
- scottyHApp :: MuConfig IO -> ScottyH () -> IO Application
Runners and types
scottyHOpts :: Options -> ScottyH () -> IO ()Source
The runner to use instead of scottyOpts
The DSL itself
Configuration
setHastacheConfig :: MuConfig IO -> ScottyH ()Source
Update the Hastache configuration as whole
modifyHastacheConfig :: (MuConfig IO -> MuConfig IO) -> ScottyH ()Source
Modify the Hastache configuration as whole
setTemplatesDir :: FilePath -> ScottyH ()Source
Set the path to the directory with templates. This affects
how both hastache
and the {{> template}}
bit searches for the
template files.
setTemplateFileExt :: String -> ScottyH ()Source
Set the default extension for template files. This affects
how both hastache
and the {{> template}}
bit searches for the
template files.
Actions
hastache :: FilePath -> ActionT HState ()Source
This is a function, just like html
or text
.
It takes a name of the template (the path is computed using the
information about the templates dir and template files extension)
and renders it using Hastache.
The variables that have been initialized using setH
are
substituted for their values, uninitialized variables are
considered to be empty/null.
Internals
mkHStateRunners :: MuConfig IO -> IO (forall a. HState a -> IO a, HState Response -> IO Response)Source
scottyHApp :: MuConfig IO -> ScottyH () -> IO ApplicationSource