Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created October 10, 2012 11:47
Show Gist options
  • Save itsjavi/3865108 to your computer and use it in GitHub Desktop.
Save itsjavi/3865108 to your computer and use it in GitHub Desktop.
jQuery template plugin (using Mustache and data-template elements for templates)
/*
* Sample:
* HTML:
* <div data-template="greeting">hi, {{name}}</div>
*
* JavaScript:
* $("div").tpl("greeting", {'name':'Paul'});
*/
$.fn.tpl = function(name, vars, partials){
return $(this).tplParse($('*[data-template="' + name + '"]:first').html(), vars, partials);
}
/*
* Sample:
* JavaScript:
* $("div").tplParse("hi, {{name}}", {'name':'Paul'});
*/
$.fn.tplParse = function(template, vars, partials){
var _tpl = Mustache.to_html(template, vars, partials);
return this.not('*[data-template]').each(function(){
$(this).html(_tpl);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment