Skip to content

Instantly share code, notes, and snippets.

@d13
Created July 29, 2015 03:25
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 d13/95f237fbbb9f6802b932 to your computer and use it in GitHub Desktop.
Save d13/95f237fbbb9f6802b932 to your computer and use it in GitHub Desktop.
Quick n' Dirty Mustache Variable Replacement
/**
* Function that replaces mustache variables in a string with values from the data object
* @param {String} tmpl String with the variables to be replaced
* @param {Object} data Object with data to be populated
* @return {String} Returns a string with mustache variables replaced
*/
var buildTmpl = function(tmpl, data) {
if (!data) {
return tmpl;
}
return tmpl.replace(/\{\{(.*?)\}\}/g, function(mustache) {
var keyName = mustache.replace(/[\{\}]*/g, '');
return data[keyName] || '';
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment