ericf / express-handlebars

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

How to define a global helper? #93

Closed ricardograca closed 9 years ago

ricardograca commented 9 years ago

The documentation about helpers says that:

The following example shows helpers being specified at each level

But in reality it only shows examples of helpers being defined on instance and render levels.

I know I can use the registerHelper method of Handlebars, however express-handlebars doesn't seem to have any registerHelper methods on the object returned by require('express-handlebars'). After looking in the source code it seems the Handlebars object is available at ExpressHandlebars.prototype.handlebars, so is the only way to register a global helper something like this:

var ehb = require('express-handlebars')
ehb.prototype.handlebars.registerHelper('myHelper', function() { /* ... */})
ericf commented 9 years ago

There are two options to gain access to Handlebars:

1) Depend on the handlebars package in your app, and pass a ref along to Express Handlebars using the handlebars config option

2) Create an Express Handlebars instance, then access Handlebars via the handlebars prop.

Using either of these approaches will give you access to the Handlebars object on which you can call registerHelper() to add a global helper.

ricardograca commented 9 years ago

Thanks for your answer. What about the issue of the documentation being incomplete? Do you want me to create a pull-request to fix that?

ericf commented 9 years ago

@ricardograca Yeah that would be great, thanks!