Is it possible to "tie" the helper to the render? (using express & hbs)

I have a helper function that looks like this:

hbs.registerHelper('feature', function(request, flag, options) { if (features(flag, request)) { return options.fn(this); } else if (options.inverse) { return options.inverse(this); } }); 

And used in the template again and again:

 {{feature request "some-feature"}} ... {{/feature}} 

I would like to remove the request part in the template, since it always has the same value and never changes. Therefore, I assume that I could bind request to feature when rendering, and obviously this changes every time, and I don't want it to spill onto another request.

Something like:

 res.render("page", { feature: hbs.helper.feature.bind(null, req) }); 

Is it possible?

+7
source share
1 answer

If you are not using the well-known helper mode, then an auxiliary evaluation will check the context so that you can pass the binding as it was above, and it should work.

According to the latest code in handlebars master, eval looks something like this:

 helper = helpers.foo || (depth0 && depth0.foo) || helperMissing helper.call(depth0, 1, {"name":"foo","hash":{},"data":data} 

Where depth0 is the current context object. The caveat here is that helpers get priority, so you need to name them differently. You should also do something like {{./foo bar}} to give priority to the local version of the context, but it looks like we have an error where it isn is not executed in this particular syntax construct.

+2
source share

All Articles