Skip to content

Instantly share code, notes, and snippets.

@magus
Created April 30, 2011 01:41
Show Gist options
  • Save magus/949325 to your computer and use it in GitHub Desktop.
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
/**
* 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