Created
November 23, 2010 23:50
-
-
Save millermedeiros/712798 to your computer and use it in GitHub Desktop.
Simple string template parsing - AS3
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
/** | |
* Simple string replacement inspired by Mustache | |
* @param template String to be parsed | |
* @param data Object containing Key -> Value pairs. | |
* @return Formated string | |
*/ | |
function parseTemplate(template : String, data : Object): String { | |
function replaceFn() : String{ | |
var prop : String = arguments[1]; | |
return (prop in data)? data[prop] : ''; | |
} | |
return template.replace(/{{(\w+)}}/g, replaceFn); | |
} | |
// | |
// Usage Example: | |
// | |
// var table : Object = { name : "lorem ipsum", date : "2010/11/23" }; | |
// var template: String = "{{name}} - {{date}}"; | |
// | |
// trace(parseTemplate(template, table)); // -> lorem ipsum - 2010/11/23 | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mustache light! Haha.