Skip to content

Instantly share code, notes, and snippets.

@binary10ve
Created February 3, 2014 10:46
Show Gist options
  • Save binary10ve/8781867 to your computer and use it in GitHub Desktop.
Save binary10ve/8781867 to your computer and use it in GitHub Desktop.
Class for creating Html chunks on the fly
function Template( html, object_s ){
this.html = html,
this.object_s = object_s,
this.parser = function( obj ){
var html = this.html;
for( var i in obj ){
// Create a regex out of object key and replace it with their values
var reg = new RegExp( "\{" + i + "\}" , "g" );
var html = html.replace( reg, obj[i] );
}
return html;
},
this.collection = function(){
var output = [];
for( var i = 0, l = this.object_s.length; i < l ;i++ ){
output.push( this.parser(this.object_s[i]) );
}
return output.join( "" );
},
this.locals = function(){
return this.parser( this.object_s );
},
this.render = function(){
// This is Nasty
return ( this.object_s instanceof Array ) ? this.collection() : this.locals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment