Skip to content

Instantly share code, notes, and snippets.

@yeco
Forked from OscarGodson/parseTemplate.js
Created December 23, 2011 00:02
Show Gist options
  • Save yeco/1512399 to your computer and use it in GitHub Desktop.
Save yeco/1512399 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