npm install
See examples
Build examples:
node ./bin/temple examples/*temple # Dump on stdout
node ./bin/temple examples/*temple > templates.js # Dump into templates.js
Load scripts [temple_utils.js, templates.js]
Or cat templates.js temple_utils.js > res.js
and paste it into chrome console
console.log(JSON.stringify(window.templates_pool.info())); # pool is empty
var t = window.templates_pool.get("ss"); # Load empty ss template
console.log(JSON.stringify(window.templates_pool.info())); # pool has one busy ss item
console.log(t[0]); # Look as dom
t[1].A("FiRsT");
console.log(t[0]);
t[1].B([{C: 1, E: "Yahhhoo!"},{C: 2, E: "MMMM",D: [1,1,1,9]}]);
console.log(JSON.stringify(window.templates_pool.info())); # more busy templates
window.templates_pool.build_cache({"ss": 100}); # build 100 elements cache for ss template
console.log(JSON.stringify(window.templates_pool.info())); # fresh new 100 ss items ready for action
console.log(t[0]);
For example, your template file my_template.temple
looks like:
<div id="{{id}}">
{{name}}
</div>
Build temple functions from template:
node path/to/temple/bin/temple my_template.temple > templates.js
Also don't forget include temple_utils.js
to your page, head section must look like:
<script src="temple_utils.js"></script>
<script src="templates.js"></script>
After that you'll have templates
variable with all your templates and temple manipulations methods.
Templates named by filename, for example you get templates.get('my_template')
.
For example, you want render simple information:
data = {
"id": 1,
"name": "John"
}
Don't forget that json must be valid, you can try validator first. And finnaly pass data to your template:
myTemplate = templates.get('my_template', data);
or
pool = templates.get('my_template')[1];
myTemplate = pool.update(data);
Variable myTemplate
its array with [0]
DOM template and [1]
temple methods for template.
div = document.getElementById('place-for-append')
div.appendChild(myTemplate[0])
Temple templates are valid XML-tree:
<div id="{{id}}">
{{name}}
</div>
You can use forall
instruction for render each item of array:
<ul>
<forall key="items">
<li>{{value}}</li>
</forall>
</ul>
And use if
for simple conditions:
<div>
<if key="plane">
Flight number: {{airline}} {{number}}
</if>
<if key="train">
Train number: {{number}}
</if>
</div>
Use include
to include partial template:
<include name="foo" key="value"/>
where foo
is template name, and value
is data for rendering;
myTemplate = templates.get('my_template');
myTemplate = templates.get('my_template', {data: data});
templates.info().free
templates.build_cache({‘my_template’: 1000})
templates.info().free
Return DOM element
Temple provide setters for child template, for template my_template
<ul>
<forall key="items">
<li>{{data}}</li>
</forall>
</ul>
You'll have
myTemplate = templates.get('my_template')[1]
myTemplate.items([{"data": "some data"}, {"data": "some data2"}])