Maintainer | bartek@skrivapa.se |
---|---|
Stability | development |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
This is main templating module. It provides basic interface for
generation templates (RenderTemplate) with renderTemplate
function. We also provide types for templates and few functions for
loading and testing
HOW TO USE TEMPLATING SYSTEM
1) There is a folder called templates, All templates files (*.st) are there. Each files contains many templates definitions, and there must be line starting with # between each two templates. Template definition has form 'nameOfTemplate=bodyOfTemplate'.
2) Template body is just String Template and you should be able to find more info at http://www.haskell.org/haskellwiki/HStringTemplate
3) All templates are in a global scope. Watch out for conflicting names. On dev computers they are loaded on every request, so one can change them without stoping server.
4) To generate a template in haskell call renderTemplate.
First param is a set of all templates. Usually you can get it from Context
.
Next is a name of template that You wan't to render.
Last one is some for of list of params.
As a result you get IO String.
If template will fail You will get error info inside. But this is only for syntax errors.
If You will forget a param there will be info in log, and template set param value to something empty.
FIELDS
Current policy is to use fields. You can find usage of [(String,String)] as params and also composition of setAttribute functions in a code. This are old concepts and will be droped at some point.
How to user fields:
- there is one function (field
) that sets one field
- fields form a monad so you can use do notation for setting many fields
- value of a field can be almoust everything (String, Int, Maybe, List, Map, types that are instances of Data and Typeable etc)
- IO wrapped values and fields can be also a values of a field.
Example
userView tempates user = renderTemplate templates "userView" $ do userFields userFields user = do field "name" $ username user field "company" $ usercompany user field "documents" $ map (documentFields) getUserDocumentsFromDB documentFields document = do field "id" $ documentid document field "title" $ documenttitle document
Why we want to use fields
- They force reuse. We write documentFields, and reuse it every time we want to pass document info to template.
- Fields can be extended. If I want to have extended info about user I use userFields
to set basic info and
then add advanced fields
- No need to first bind from IO, then pass to template
- They support advanced structures like lists and maybe's
Some extra info: In templates use maybe. You can use 'if' in template body to check for Nothing Always change ByteString to String. We have a problems with encoding, so please watch for this.
Please also see example.hs for a running example
Synopsis
- data Fields m a
- runFields :: Monad m => Fields m () -> m [(String, SElem String)]
- class (Applicative m, Monad m) => TemplatesMonad m where
- getTemplates :: m Templates
- getTextTemplatesByLanguage :: String -> m Templates
- renderTemplate :: TemplatesMonad m => String -> Fields m () -> m String
- renderTemplate_ :: TemplatesMonad m => String -> m String
- renderTemplateI :: TemplatesMonad m => String -> Fields Identity () -> m String
- newtype TemplatesT m a = TemplatesT {
- unTT :: InnerTemplatesT m a
- runTemplatesT :: (Functor m, Monad m) => (String, GlobalTemplates) -> TemplatesT m a -> m a
- renderHelper :: Monad m => Templates -> String -> Fields m () -> m String
Documentation
Simple monad transformer that collects info about template params
Instances
MonadTrans Fields Source # | |
Defined in Text.StringTemplates.Fields | |
MonadTransControl Fields Source # | |
MonadBase b m => MonadBase b (Fields m) Source # | |
Defined in Text.StringTemplates.Fields | |
MonadBaseControl b m => MonadBaseControl b (Fields m) Source # | |
Monad m => Monad (Fields m) Source # | |
Functor m => Functor (Fields m) Source # | |
Monad m => Applicative (Fields m) Source # | |
MonadThrow m => MonadThrow (Fields m) Source # | |
Defined in Text.StringTemplates.Fields | |
MonadCatch m => MonadCatch (Fields m) Source # | |
MonadMask m => MonadMask (Fields m) Source # | |
Defined in Text.StringTemplates.Fields | |
type StT Fields m Source # | |
Defined in Text.StringTemplates.Fields | |
type StM (Fields m) a Source # | |
Defined in Text.StringTemplates.Fields |
runFields :: Monad m => Fields m () -> m [(String, SElem String)] Source #
get all collected template params
class (Applicative m, Monad m) => TemplatesMonad m where Source #
Simple reader monad class that provides access to templates
getTemplates :: m Templates Source #
Get templates (for text templates default language name is used).
getTextTemplatesByLanguage :: String -> m Templates Source #
Get templates (for text templates specified language name is used).
Instances
(Applicative (t m), Monad (t m), MonadTrans t, TemplatesMonad m) => TemplatesMonad (t m) Source # | Generic, overlappable instance. |
Defined in Text.StringTemplates.Templates.Class getTemplates :: t m Templates Source # getTextTemplatesByLanguage :: String -> t m Templates Source # | |
(Applicative m, Monad m) => TemplatesMonad (TemplatesT m) Source # | |
Defined in Text.StringTemplates.Templates |
:: TemplatesMonad m | |
=> String | template name |
-> Fields m () | template params |
-> m String |
renders a template by name
:: TemplatesMonad m | |
=> String | template name |
-> m String |
renders a template by name without any params
:: TemplatesMonad m | |
=> String | template name |
-> Fields Identity () | template params |
-> m String |
renders a template by name (params function cannot use side effects)
newtype TemplatesT m a Source #
Simple implementation of TemplatesMonad
TemplatesT | |
|
Instances
:: (Functor m, Monad m) | |
=> (String, GlobalTemplates) | (default language name, global templates) |
-> TemplatesT m a | |
-> m a |