patrickkettner / grunt-compile-handlebars

grunt plugin to compile static html from a handlebars plugin
MIT License
115 stars 29 forks source link

Calling Helper from another Helper (or, how do I get a hold of the original handlbars instance) #31

Closed MichielVdAnker closed 9 years ago

MichielVdAnker commented 10 years ago

I am trying to call a helper from another helper. But this isn't working. I think this problem is related to https://github.com/patrickkettner/grunt-compile-handlebars/issues/11

I have a Helper called 'page-helper' which I use as a sort of partial but with the block syntax so I go:

{{#page-helper}}
    {{>page-content context1}}
    {{>page-content context2}}
{{/page-helper}}

Inside this helper I construct a DIV and render the content inside e.g.:

module.exports = function (context, option) {
    return '<div class="page" ' +
            'data-uuid="' + <I WANT TO CALL uuid-helper HERE> + '">' +
            context.fn(this) +
            '</div>';
};

But I can't find a way of doing this, partly because I can't get to the Handlebars instance that holds the registered helpers. Doing things things as suggested in https://github.com/patrickkettner/grunt-compile-handlebars/issues/11 doesn't work in this case, because it looks to be a fresh Handlebars instance.

Maybe I'm just abusing Handlebars, in which case I am open for critique :)

patrickkettner commented 10 years ago

Hey @MichielVdAnker! Sorry, I am not 100% certain I understand what you are trying to do, but I think you are looking for subexpressions

Would something like this be about what you are looking for?

MichielVdAnker commented 10 years ago

Hi @patrickkettner

Thanks for the response. Your example demonstrates precisely what I am trying to do, except that my helpers and partials are in seperate files following the "globbedTemplateAndOutput" example.

Having looked into sub-expressions, I think they can provide a solution. So, many thanks for that already!

Just to clarify my problem some more though: In the case of partials I have no problems, all the globbed partials and helpers are available, but in the scope of a helper none of them seem accessible. Creating a new Handlebars instance does not give me access to all the partials and helpers. When I take a peek into "compile-handlebars.js" I think I see an instance of Handlebars that I would want to have access to.

patrickkettner commented 10 years ago

I'll look into this more in the next couple of days

re "I think I see an instance of Handlebars that I would want to have access to." - you can pass in a handlebars instance, otherwise it just loads one that is bundled with the plugin.

MichielVdAnker commented 9 years ago

@patrickkettner thanks for the support; sub-expressions are totally enough to work with for now. Thank again.