Stpl.js
All exemple in this document will use following family object defined here:
Defining a template string
Using a script tag
To define a template string just create a
script tag with
type="text/stpl" and a template name in the rel attribute.
For example writing this:
define a template named "templateName" with "this is my template {{withVar}}" as the template String where withVar will be replaced by the content of the given withVar property of given datas
Using registerString method
You also can directly register a template string using the
registerString method
stpl.registerString('templateName', 'this is my template {{withVar}}');
Simple variable replacement
with this template defined
We can perform replacement and put it inside a div just like this:
document.getElementById('divFamilyDesc').innerHTML = stpl("familyDesc",family);
this will lead to the following result:
pointing dotted properties
Template defined:
Javascript replacement:
document.getElementById('divMotherAndDad').innerHTML = stpl("motherAndDad",family);
give the following result:
Looping through child elements
to get a listing of the family's childs we can perform as follow:
code in the page
Javascript replacement:
document.getElementById('childsList').innerHTML = stpl("childs",family);
give the following result:
pointing property of parents elements
modifying the previous example like this:
code in the page
Javascript replacement:
document.getElementById('childsList').innerHTML = stpl("childs",family);
give the following result:
Conditional