ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.32k stars 382 forks source link

Partial rendered from inside "extend" helper can't see helpers #219

Closed shukriadams closed 6 years ago

shukriadams commented 6 years ago

Inside a keystone.js app, I'm using a helper that renders a partial by name. The rendering works, but the partial seems to missing global context - I cannot call any custom helpers from it.

_helpers.render = function (partialName, data, options) {
    var template = options.data.exphbs.partials[partialName];

    if (!template)
        throw new Error('Missing partial ' + partialName );

    return template(data);
};

And I call this with

{{#render "myPartial" someData}} {{/render}}

Anyone know what I'm missing? I'm actually trying to do something more complex but for the sake of brevity all I need to get working is the above.

shukriadams commented 6 years ago

Indirectly found a clue from the _renderTemplate : function(template, context, options) hook.

the fix is

_helpers.render= function (partialName, data, options) {

    var template = options.data.exphbs.partials[partialName];
    if (!template)
        throw new Error('Missing partial ' + partialName );

    return template(data, options.data.exphbs, options);
};

Turns out the required engine context is options.data.exphbs, as all the helpers are attached to that object.