mattacular / grunt-handlebars-compiler

Grunt.js task to precompile Handlebars.js templates with the same options as the CLI utility
MIT License
12 stars 12 forks source link

Additional AMD option to return the template rather than extending the Handlebars.templates object #8

Closed TheSisb closed 9 years ago

TheSisb commented 10 years ago

In my project I am compiling my templates for use within an AMD (require.js) context.

In a module, in order to use a compiled template, I do something like this:

define(["jquery", 'Handlebars', "templates/contextMenu"], function($, Handlebars){

    var ctxMenu = {
        contextMenu: {},
        ctxTmpl: Handlebars.templates['contextMenu'],
                ...

I was wondering about the feasibility and your thoughts on modifying the compiled output as to allow something like this:

define(["jquery", "templates/contextMenu"], function($, template){

    var ctxMenu = {
        contextMenu: {},
        ctxTmpl: template,
                ...

This can be achieved quite simply by changing the compiled template from:

define(['Handlebars'], function (Handlebars) {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['contextMenu'] = template(function (Handlebars,depth0,helpers,partials,data) {

to

define(['Handlebars'], function (Handlebars) {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
return template(function (Handlebars,depth0,helpers,partials,data) {

Looking forward to your thoughts on this.

mattacular commented 9 years ago

PR was merged a while back that satisfies this issue. Thanks again :D