Created
April 30, 2011 01:41
-
-
Save magus/949325 to your computer and use it in GitHub Desktop.
templating function which can traverse the path of a context object from a string representation
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 Templating Function | |
* ========================== | |
* Replace {variable} in template string | |
* Able to resolve paths in template variable strings based on | |
* the context provided as second argument. | |
* | |
* Inspired by Thomas Fuchs | |
* http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/ | |
* | |
* By Noah M. Jorgenson | |
* (Not quite tweet-sized: 198 characters) | |
*/ | |
function t(b, g) { | |
for (var c = b.match(/\{([^\}]*)}/g), a = 0; a < c.length; a++) { | |
for (var f = c[a].substring(1, c[a].length - 1).split("."), d = g, e = 0; e < f.length; e++) d = d[f[e]]; | |
b = b.replace(RegExp(c[a], "g"), d) | |
} | |
return b | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment