Copyright | (c) Dzianis Kabanau 2017 |
---|---|
Maintainer | kobargh@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
This is a Haskell implementation of Template Toolkit - the popular Perl template processing system.
Documentation
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('|') %]