Created
November 20, 2015 15:53
-
-
Save essoen/f70eb8799b68a6537d8e to your computer and use it in GitHub Desktop.
Handlebars-helper
This file contains hidden or 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
'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