Created
May 21, 2013 14:07
-
-
Save babakhani/5620045 to your computer and use it in GitHub Desktop.
Micro mustache plugin
This file contains hidden or 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
$.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