Created
July 29, 2015 03:25
-
-
Save d13/95f237fbbb9f6802b932 to your computer and use it in GitHub Desktop.
Quick n' Dirty Mustache Variable Replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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