Created
November 20, 2009 08:47
-
-
Save ThisIsMissEm/239361 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(){ | |
/** | |
* Class Template. | |
* This class is used for generating and rendering templates. | |
*/ | |
var undefined; | |
var templater = this.templater = { | |
/** | |
* Renders a new template, given options. | |
* @param options Object | |
* @param options[template] The template to use. | |
* @param options[data] The data to apply to the template. | |
* @return string (html) | |
*/ | |
render: function(templateName, data){ | |
if(this._templates[templateName] == undefined){ | |
throw new Error("Invalid Template Name."+templateName); | |
return; | |
} | |
var template = this._templates[templateName]; | |
if(Object.prototype.toString.call(template) === '[object Array]'){ | |
template = template.join(''); | |
} | |
template = template.replace(/#\{(.*?)\}/gi, function(str, p1){ | |
return data[p1] ? data[p1] : '#{'+p1+'}'; | |
}); | |
return template; | |
}, | |
/** | |
* Adds a template from storage | |
* @param name Name of the template | |
* @param data The data that should be render for the template | |
*/ | |
create: function(name, data){ | |
this._templates[name] = data; | |
}, | |
/** | |
* Removes a template from storage | |
* @param name Name of the template | |
*/ | |
destroy: function(name){ | |
delete this._templates[name]; | |
}, | |
/** | |
* Storage object for all templates. | |
*/ | |
_templates: { | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment