Skip to content

Instantly share code, notes, and snippets.

@amitkumar
Last active August 29, 2015 13:56
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 amitkumar/9008296 to your computer and use it in GitHub Desktop.
Save amitkumar/9008296 to your computer and use it in GitHub Desktop.
Precompile all handlebars templates on page load
// Assumes jQuery
var handlebarsTemplates = {};
$(function(){
// Compile all templates
$('script[type="text/x-handlebars-template"]').each(function(index, element) {
var $element = $(element),
partialName = $element.attr('data-register-partial-as');
self.handlebarsTemplates[element.id] = Handlebars.compile($element.html());
if (partialName) {
Handlebars.registerPartial(partialName, $element.html());
}
});
// Use
var data = {};
var html = handlebarsTemplates['my-template'](data);
});
script(id="my-template", type="text/x-handlebars-template")
script(id="my-partial-template", type="text/x-handlebars-template", data-register-partial-as="myPartial")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment