Skip to content

Instantly share code, notes, and snippets.

@essoen
Created November 20, 2015 15:53
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 essoen/f70eb8799b68a6537d8e to your computer and use it in GitHub Desktop.
Save essoen/f70eb8799b68a6537d8e to your computer and use it in GitHub Desktop.
Handlebars-helper
'use strict';
var handlebars = function() {
/**
*
* @param templateName
* @param containerName
* @param context
*/
var setup = function(templateName, containerName, context) {
var source = $(templateName).html(); // Get the HTML source of the template
var template = Handlebars.compile(source); // Compile it
$(containerName).html(template(context)); // replace HTML in the container
};
// Helper function that can pass contextual values to methods as json
// Needed because Handlebars converts the values to strings before they're shown in the context
Handlebars.registerHelper('json', function(context) {
return JSON.stringify(context).replace(/"/g, '"');
});
return {
setup: setup
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment