Skip to content

Instantly share code, notes, and snippets.

@d13 d13/buildTmpl.js
Created Jul 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.