ericf / express-handlebars

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

Express locals as options in public API #27

Closed MarcDiethelm closed 11 years ago

MarcDiethelm commented 11 years ago

It's not possible to pass app.locals or res.locals as template context/rendering options into the public API methods. The methods that accept options perform a check if the argument is a function, and if it is it's treated as a callback. app.locals and res.locals are function too however. Unfortunately Express doesn't offer a way of getting all the locals' properties at once.

In order to use Express locals the exphbs API one is forced to copy the properties over to a fresh object.

ericf commented 11 years ago

Which methods are you trying to pass app.locals and res.locals into?

ericf commented 11 years ago

I could modify the arguments check in the public methods to be:

if (typeof options === 'function' && options.name !== 'locals') {
    callback = options;
    options  = {};
}

This should work since app.local and res.locals are functions with the name "locals". What do you think?

MarcDiethelm commented 11 years ago

That sound like a plan! You could additionally check if options.arity == 1 but that's likely overkill.

For the record I am using the render method, here: https://github.com/MarcDiethelm/xtc/blob/develop/controllers/_default.js#L79

ericf commented 11 years ago

@MarcDiethelm let me know if #28 looks good to you, and if you could try it out locally that would be excellent. If it looks good, I'll publish this to npm.

MarcDiethelm commented 11 years ago

I tested it, and it's looking good!

ericf commented 11 years ago

@MarcDiethelm the changes in #28 have been published to npm as v0.5.0.