Last active
August 29, 2015 14:25
-
-
Save sshadmand/c5a4325c3ecb7998937b to your computer and use it in GitHub Desktop.
Jquery Handlebars Rendering Extension
This file contains 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
/* | |
* 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