serve-index
serve-index copied to clipboard
Provide method to add to template locals
Hey, would be great to have req and res added to the arguments passed back through, when using the template as a function.
render(locals, function (err, body) {
if (err) return next(err);
var buf = new Buffer(body, 'utf8');
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Content-Length', buf.length);
res.end(buf);
});
So would look like:
render(locals, function (err, body) {
if (err) return next(err);
var buf = new Buffer(body, 'utf8');
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Content-Length', buf.length);
res.end(buf);
},req, res);
Hi! The point of the rendering is to be independent of any interaction and creating side-effects from the request, thus why we do not provide the req and res. Can you explain the use-case you have for adding these, and we can consider the best approach to achieving your goal?
Hi, so the situation I'm in is, I need to get session information that is on the req object. In this instance, it's the current users username. Thanks for you help
Gotcha. So, to me, that sounds like what you really want is the ability to add information to the locals you are provided, so you can provide the user name to the rendering function.
That could work as well. :) :+1:
Cool :) So the interface I'm thinking of is the following: the provided locals will be the mixture of a locals option, req.locals, and the required values for the template to function.
The idea is that anything based on the request you would simply store in req.locals. If you are using Express, this is already there and the way you already interact with templates from middleware, so hopefully this should make sense.
Then, especially once we start using a real template engine, it may be useful to provide locals that are static for the middleware, perhaps because the template is used across multiple middleware, etc. This would be what the locals option to the middleware would provide.
Finally, all the properties that are in the locals variable today would be mixed in on top of this.
@dougwilson Sorry never got back to you on this. Has there been any progress on adding this feature?
Thanks.
Hi @adrianbrowning, no, I don't think there has been any progress on this. If you were thinking someone else may drop in to do this, sorry, I didn't realize, and probably other passers by didn't either :) I'll add a help wanted label to this.
Ah thank you @dougwilson