jsTemplateBuilder 1.2 Documentation
===================================
What is jsTemplateBuilder ?
----------------------------
jsTemplateBuilder could generate content (XUL in particulary) from a template file and
javascript datas. The generated content will be inserted into a document.
So you give to the template builder :
- a template file that contents tags to generate and some instructions to the template engine
- some datas that are assigned to internal variables, which will be used by the template engine. this
datas may be some values to insert into attribute, some arrays/objects from which the template
engine will iterate etc..
Template format
---------------
The template file is an xml file. Here is a minimum version :
In this file, you'll insert some tags that will be generated (XUL tags or other format),
and some specific tags that represent statements for the template engine.
Template tag language
---------------------
legend :
exprvar : replace it by the name of an internal data or a javascript expression
var : replace it by the name of an internal data
expression : replace it by a simple javascript expression
string : some simple text
Javascript expression can be reference to some internal data of course
insert into the generated content a text node with the value of exprvar
add an attribute "string" to the last generated tag.
...
Simple if statement.
If you want to have an if..then..else statement, use and tags as child.
...content generated if the expression result is true...
...content generated if the expression result is false...
...
foreach statement. It generate content for each item or properties of the "var1"
internal variable (array or object). Each item or property value will be put
into var2 internal variable, and its key/name will be put into var3 internal variable
create/modify an internal variable. its name is 'var' and its value is exprvar.
dump the value exprvar to the console, and/or a simple string (the label attribute)
Used to debug.
You have also an attribute that you can use into tags that will be generated.
Exemple :
It add an "attr" attribute (replace "attr" by that you want) to the xul tag "tag", with the
"exprvar" value.
If you want add more attributes, use tag.
Example of a template file
--------------------------
Using jsTemplateBuilder in javascript
-------------------------------------
After you have written your template file, you can generate content with
jsTemplateBuilder object.
// get an instance of the template engine
var jstpl = new jsTemplateBuilder();
// create some internal variable
jstpl.assign('message','Bonjour !');
jstpl.assign('othermessage', ['lorem','ispum','dolor']);
jstpl.assign('firsttest',0);
jstpl.assign('desactive','true');
jstpl.assign('firstname', ['Laurent','Daniel','Tristan']);
// an other way to create some internal variable : you can have an object with some properties
that will be the internal variables :
var datas = {
message:'Bonjour !',
othermessage: ['lorem','ispum','dolor'],
firsttest:0,
desactive:'true',
firstname : ['Laurent','Daniel','Tristan'],
}
// and give this object during the instanciation
var jstpl = new jsTemplateBuilder(datas);
// Then you can generate content using the "build" method
jstpl.build( letemplate , element , vider);
/*
letemplate = the url of the template file
element = the id of the element where the generated content will be insert into.
you can also give directly the element
vider = a boolean to indicate if you want to remove all children elements of "element"
example :
*/
jstpl.build( 'mytemplate.jstpl' , "myBox" , true);
// or
var elt=document.getElementById('myBox');
jstpl.build( 'mytemplate.jstpl' , elt , true);
Example of result
-----------------
With our example, the generated content will be
Bonjour !