public
Last active — forked from /tinystache.js

  • Download Gist
tinystache.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
var template = function(){
var args = Array.prototype.slice.call(arguments),
res = "";
console.log (args.length);
if (args.length > 1) {
data = args[ args.length - 1 ]
for ( var i = 0, l = args.length - 1; i < l; i++ ){
temp = args[i];
if (temp.constructor.toString().indexOf("Array") != -1) {
temp[temp.length] = data
res += template.apply( this, temp );
} else {
res += args[i].replace( /\{{2}([^{]*)\}{2}/g, function(string, parens) { return data[parens]; } )
};
};
return res
} else {
throw "Need 1 or more template files and one data hash"
}
}
 
var exampleTemplate = [
"<html>",
" <title>{{title}}</title>",
" Hi {{name}}, it's {{day}}",
"</html>"
];
 
var exampleData = {title: "Some Web Page", name: "Giles", day: "Monday"};
 
console.log(template( exampleTemplate, exampleData ));
console.log(template( "<html>"," <title>{{title}}</title>"," Hi {{name}}, it's {{day}}","</html>", exampleData ));
console.log(template( ["<html>"," <title>{{title}}</title>"], " Hi {{name}}, it's {{day}}","</html>", exampleData ));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.