Introduction

TPL is a template engine for generating (X)HTML, XML, text, ... It is based on libtemplate. The source of libtemplate is ported to wxWidgets. It is this updated version that is ported to JavaScript.

A template engine allows you to separate the logic of your application from the view. A template consists of literal text and elements. An element will be replaced by a value set in the template engine. When a template is parsed, the elements are replaced with their set values, and the parsed template is stored in an new element. The developer has the option of replacing any existing value for the template, or appending to the existing value.

Template files

A template file can contain multiple templates. Each template must be enclosed between <template> and </template> tags. It has a mandatory name attribute. Quotes arround the attribute value is mandatory. Within a template, there is literal text, which will be unchanged by the parsing process, and elements. Parsing will replace the element with the values assigned in the template engine. Elements without assigned values will not appear in the parsed result.

An element is enclosed in curly braces, and may contain letters, numbers and punctuation marks. Whitespace between curly braces will cause it to be interpreted as literal text rather than an element. This convention allows the use of javascript functions in templates.

A template example

<template name="grid">
  Base	Square	Cube
  ----	------	----
  {rows}
  ----------------------------
  
  The quotes "{empty}" should be empty.
</template>

<template name="row">
 {base}	{square}	{cube}
</template>

This script uses the template:

var engine = new tpl.Engine();
var n = 1;

// Load the template file
engine.load("cube.tpl");
for(n = 1;n <= 10; n++) 
{
 engine.base   = n;
 engine.square = n * n;
 engine.cube   = n * n * n;
 // Parse the template 'row' and add the result to element 'rows'
 engine.parse("row", "rows", true);
}

engine.parse("grid", "main", false);

print(engine.main);



SourceForge.net Logo Support This Project

All Classes

tpl.Engine

Design downloaded from Zeroweb.org: Free website templates, layouts, and tools.