Last active
August 29, 2015 14:01
-
-
Save simodima/0b139fdc746a88161a49 to your computer and use it in GitHub Desktop.
superminimal javascript templating
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
String.prototype.render = function(data){ | |
return this.replace( | |
/\{([^{}]*)\}/g, | |
function(match, group){ | |
var value = data[group]; | |
if( typeof value === 'function' ) { | |
return value.apply(data); | |
} else if(typeof value === 'number' || typeof value === 'string') { | |
return value; | |
} | |
return match; | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment