Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created March 27, 2012 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaneriley/2214424 to your computer and use it in GitHub Desktop.
Save shaneriley/2214424 to your computer and use it in GitHub Desktop.
Selleck
function selleck(tmpl, obj, chain) {
chain = chain || "";
for (var v in obj) {
if (typeof obj[v] === "object") {
tmpl = selleck(tmpl, obj[v], chain + v + ".");
}
tmpl = tmpl.replace((new RegExp("{{" + chain + v + "}}", "g")), obj[v]);
}
return tmpl;
}
// Usage:
// var tmpl = "<div class='{{mood.type}}'><img src='/images/{{icon.src}}' /></div>";
// var obj = { mood: { type: "ecstatic" }, icon: { src: "icon_ecstatic.png" } };
// var html = selleck(tmpl, obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment