An ultralight and super fast templating framework
npm install templates.js
var parsed = templates.parse(template, data);
`
$3
`
{quote.text}
{quote.author}
`
$3
`
var express = require('express'),
app = express(),
templates = require('templates.js'),
data = {};
app.configure(function() {
app.engine('tpl', templates.__express);
app.set('view engine', 'tpl');
app.set('views', 'path/to/templates');
});
app.render('myview', data, function(err, html) {
console.log(html);
});
app.get('/myview', function(res, req, next) {
res.render('myview', data);
});
`
Template Syntax
Sample data, see test cases for more:
`
{
"animals": [
{
"name": "Cat",
"species": "Felis silvestris catus",
"isHuman": false,
},
{
"name": "Dog",
"species": "Canis lupus familiaris",
"isHuman": false,
},
{
"name": "Human",
"species": "Homo sapiens",
"isHuman": true
}
],
"package": {
"name": "templates.js",
"author": "psychobunny",
"url": "http://www.github.com/psychobunny/templates.js"
},
"website": "http://burnaftercompiling.com",
"sayHello": true
}
`
$3
`
My blog URL is {website}. The URL for this library is {package.url}
`
$3
`
Hello world!
somethingFalse doesn't exist
`
$3
Repeat blocks of HTML with an array. The two helpers @first and @last are also available as conditionals within an array.
`
{animals.name} is from the species {animals.species}.
- This could be a pet.
prints out:
Cat is from the species Felis silvestris catus.
- This could be a pet.
Dog is from the Canis lupus familiaris.
- This could be a pet.
Human is from the species Homo sapiens.
`
$3
Helpers are JavaScript methods for advanced logic in templates. This example shows a really simple example of a function called print_is_human which will render text depending on the current block's data.
`
templates.registerHelper('print_is_human', function(data, iterator, numblocks) {
return (data.isHuman) ? "Is human" : "Isn't human";
});
{function.print_is_human}
prints out:
Isn't human
Isn't human
Is human
``