Created
March 25, 2013 02:29
-
-
Save wiky/5234566 to your computer and use it in GitHub Desktop.
simple template, data substitute
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 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