Skip to content

Instantly share code, notes, and snippets.

@sshadmand
Last active August 29, 2015 14:25
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 sshadmand/c5a4325c3ecb7998937b to your computer and use it in GitHub Desktop.
Save sshadmand/c5a4325c3ecb7998937b to your computer and use it in GitHub Desktop.
Jquery Handlebars Rendering Extension
/*
* Requires Jquery and Handlebars
* Render a template by passing in data and prepend or replace a container in your DOM.
* return DOM object so you can chain subsequent actions.
* Example:
* $("#main-stage").renderTemplate("template-id", dataDict);
* OR
* $("#main-stage").renderTemplate("template-id", dataDict).fadeIn("fast");
*/
jQuery.fn.extend({
renderTemplate: function (templateName, data, insertType) {
var source = $("#" + templateName).html();
var template = Handlebars.compile(source);
var html = template(data);
if (insertType === "prepend"){
$(this).prepend(html);
} else {
$(this).html(html);
}
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment