Created
August 3, 2011 07:50
-
-
Save hejrobin/1122124 to your computer and use it in GitHub Desktop.
This file contains 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
// I moustache you a question, but I'll shave it for later... | |
String.prototype.moustache = function(object, regex) { | |
return this.replace(regex || (/\\?\{\{([^{}]+)\}\}/g), function(match, key) { | |
if(match.charAt(0) == '\\') | |
return match.slice(1); | |
return (object[key] !== null) ? object[key] : ''; | |
}); | |
}; | |
// Simple usage | |
var simple = "It's all about {{apples}} and {{oranges}}!".moustache({ | |
'apples': 'bananas', | |
'oranges': 'kiwis' | |
}); | |
// Custom regex[p] | |
var custom = "No %name%, these men are nihilists...".moustache({ | |
'name': 'Donnie' | |
}, /\\?\%([^{}]+)\%/g); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment