Skip to content

Instantly share code, notes, and snippets.

@varya
Created December 13, 2008 16:45
Show Gist options
  • Save varya/35496 to your computer and use it in GitHub Desktop.
Save varya/35496 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.applyTemplate = function(template, params, mode) {
var method = mode || 'html'
var reg = /\$\{([a-zA-Z_\.]*)\}/g;
var iter, tname, se, path, par;
while((iter = reg.exec(template)) != null) {
lastIndex = reg.lastIndex;
tname = iter[1];
path = tname.split('.');
par = params;
$(path).each(function(p){
par = par[this];
});
if (par) {
template = template.replace('${' + tname + '}', par);
}
}
//$(this).get(0).innerHTML = template;
$(this)[method](template);
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment