template-toolkit-0.1.1.0: Template Toolkit implementation for Haskell

Copyright(c) Dzianis Kabanau 2018
Maintainerkobargh@gmail.com
Safe HaskellNone
LanguageHaskell2010

Text.TemplateToolkit

Contents

Description

This is a Haskell implementation of Template Toolkit - the popular Perl template processing system.

Synopsis

Documentation

evalTemplateFile Source #

Arguments

:: TName

Template filename to process

-> TConfig

Template config and initial variables (either aeson object or JSON object string)

-> IO (Either TErr Text)

Result of template evaluation - either error or text

All variables in initial TConfig object are passed to the parsed template.

Special _CONFIG variable is an object that contains settings passed to the template evaluator: INCLUDE_PATH - list of directories where evaluator will look for template files. CACHE_DIR - path to cache templates to.

Example

Template Toolkit language manual: Text.TemplateToolkitLang.

Below is a simple example of using this module:

template-toolkit-example.hs

import Text.TemplateToolkit
import qualified Data.Text.IO as TIO (readFile)
import qualified Data.Text as T

main = do
    cfg <- TIO.readFile "./conf.json"
    s <- evalTemplateFile "template.tt" (JSONstring cfg)
    case s of
        (Right txt) -> putStr . T.unpack $ txt
        (Left err) -> putStr ("ERROR! " ++ err)

conf.json

{
  "_CONFIG":{
    "INCLUDE_PATH":["."]
  },
  "users":{
    "Foo": 13,
    "Bar": 3.14,
    "Baz": "bazzz"
  }
}

template.tt

<html>
  <body>
    <h1>Template Toolkit for Haskell</h1>
    <h2>Count 1-10:</h2>
    [% FOREACH i = [1..10] -%]
      [% i; (!loop.last) ? ', ' : '.' %]
    [%- END %]
    <h2>Users hash:</h2>
    [% FOREACH user = users.pairs %]
      <p>[% user.key %]: [% user.value %]
    [% END %]
    <h2>External template:</h2>
    [% PROCESS template2.tt words = ['dog','cat','pig'] %]
  </body>
</html>

template2.tt

<p>[% words.sort.reverse.join('|') %]

Orphan instances

Eq Val Source # 
Instance details

Methods

(==) :: Val -> Val -> Bool #

(/=) :: Val -> Val -> Bool #

Fractional Val Source # 
Instance details

Methods

(/) :: Val -> Val -> Val #

recip :: Val -> Val #

fromRational :: Rational -> Val #

Num Val Source # 
Instance details

Methods

(+) :: Val -> Val -> Val #

(-) :: Val -> Val -> Val #

(*) :: Val -> Val -> Val #

negate :: Val -> Val #

abs :: Val -> Val #

signum :: Val -> Val #

fromInteger :: Integer -> Val #

Ord Val Source # 
Instance details

Methods

compare :: Val -> Val -> Ordering #

(<) :: Val -> Val -> Bool #

(<=) :: Val -> Val -> Bool #

(>) :: Val -> Val -> Bool #

(>=) :: Val -> Val -> Bool #

max :: Val -> Val -> Val #

min :: Val -> Val -> Val #