Skip to content

Instantly share code, notes, and snippets.

@jnape
Created May 1, 2012 21:42
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 jnape/2571670 to your computer and use it in GitHub Desktop.
Save jnape/2571670 to your computer and use it in GitHub Desktop.
JavaScript Template
var Template = function(template) {
this.template = template;
this.bindings = {};
};
Template.prototype.bind = function(variable, value) {
this.bindings[variable] = value;
return this;
}
Template.prototype.parse = function(args) {
var parsedTemplate = this.template;
for (var variable in this.bindings) {
var value = this.bindings[variable];
parsedTemplate = parsedTemplate.replace("${" + variable + "}", value);
}
return parsedTemplate;
};
/**
* Usage:
*
* new Template("Hello, ${firstName} ${lastName}!")
* .bind("firstName", "James")
* .bind("lastName", "Childers")
* .parse();
* //-> "Hello, James Childers!"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment