template-toolkit-0.1.0.1: Template Toolkit implementation for Haskell

Copyright(c) Dzianis Kabanau 2017
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. For now there is only one setting: INCLUDE_PATH - list of folders where evaluator will look for template files.

Example

Template Toolkit language manual: Text.TemplateToolkitLang.

Below is a simple example of using this module:

template-toolkit-example.hs

import Text.TemplateToolkit
import 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('|') %]