Skip to content

Instantly share code, notes, and snippets.

@OscarGodson
Created June 13, 2011 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OscarGodson/1022342 to your computer and use it in GitHub Desktop.
Save OscarGodson/1022342 to your computer and use it in GitHub Desktop.
/**
* parseTemplate takes a string and a JS Object and returns a string with template
* replaced with content you provided. Template tags look like: {{tag}}
* @param {String} s This is the string to search for tags in.
* @param {Object} j This is the JS object that contains a key=>value pair for the tag and what to be replaced
* @returns {String} returns the modified string with the content you provided in replacement of the tags
* @example var html = parseTemplate('Hey, {{name}}',{ name:'John' }); //returns "Hey, John"
*/
var parseTemplate = function(s,j){
for(x in j){ var r = new RegExp('{{'+x+'}}','g'); s = s.replace(r,j[x]); }
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment