Skip to content

Instantly share code, notes, and snippets.

@ruslanchek
Created October 6, 2014 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslanchek/b9b19be31390363a4e70 to your computer and use it in GitHub Desktop.
Save ruslanchek/b9b19be31390363a4e70 to your computer and use it in GitHub Desktop.
TMPLS
var Templates = function(){
this.tmpls = {};
function renderer(html, data){
for (var p in data) {
if (data.hasOwnProperty(p)) {
html = html.replace(new RegExp('%' + p + '%', 'g'), data[p]);
}
}
return html;
}
this.add = function(name, html){
if(!this.tmpls[name]){
this.tmpls[name] = html;
}else{
console.error('Template ' + name + ' already exists!');
}
};
this.render = function(name, data){
if(this.tmpls[name]){
return renderer(this.tmpls[name], data);
}else{
console.error('Template ' + name + ' not exists!');
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment