Skip to content

Instantly share code, notes, and snippets.

@wiky
Created March 25, 2013 02:29
Show Gist options
  • Save wiky/5234566 to your computer and use it in GitHub Desktop.
Save wiky/5234566 to your computer and use it in GitHub Desktop.
simple template, data substitute
function substitute(str, obj) {
if (typeof str === 'string' && typeof obj === 'object') {
str = str.replace(/\{(\w+)\}/gi, function(s, k) {
return (typeof obj[k] === 'undefined') ? s : obj[k];
}).replace(/(\\\{)|\\\}/g, function(s, k) {
return k ? '{' : '}';
});
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment