Skip to content

Instantly share code, notes, and snippets.

@babakhani
Created May 21, 2013 14:07
Show Gist options
  • Save babakhani/5620045 to your computer and use it in GitHub Desktop.
Save babakhani/5620045 to your computer and use it in GitHub Desktop.
Micro mustache plugin
$.tmpl = function(input,dict) {
// Micro Mustache Template engine
String.prototype.format = function string_format(arrayInput) {
function replacer(key){
var keyArr =key.slice(2,-2).split("."),firstKey = keyArr[0], SecondKey = keyArr[1];
if (arrayInput[firstKey] instanceof Object){
return arrayInput[firstKey][SecondKey];
}
else{
return arrayInput[firstKey];
}
}
return this.replace(/{{\s*[\w\.]+\s*}}/g,replacer);
};
return $(input.format(dict));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment