templite

Lightweight, minimalist Javascript templating

templite

lightweight, minimalist templating

Templite parses data into html using a simple bracketed templating language. It does not handle loops and conditionals. Know your data. If the parsed value is not a string or number, templite will not render it.

// Basic example 
var html = "<h1>{{ name }}</h1><ul><li>{{ traits.age }}</li><li>{{ traits.eyes }}</li></ul>";
var data = {
    name: "brandon",
    traits: {
        age: 26,
        eyes: "brown"
    }
};
 
var tpl = templite.render( html, data );
 
 
// Templite does not do conditionals or loops 
// You need to handle that yourself, which is likely faster anyway 
var template = "<h1>{{ name }}</h1><ul><li>{{ traits.age }}</li><li>{{ traits.eyes }}</li></ul>";
var html = "";
var data = [
    {
        name: "brandon",
        traits: {
            age: 26,
            eyes: "brown"
        }
    },
    
    {
        name: "anonymous",
        traits: {
            age: 50,
            eyes: "blue"
        }
    }
];
 
for ( var i = 0, len = data.length; i < len; i++ ) {
    html += templite.render( template, data );
}