serve-index icon indicating copy to clipboard operation
serve-index copied to clipboard

Provide method to add to template locals

Open adrianbrowning opened this issue 9 years ago • 8 comments

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);

adrianbrowning avatar Jan 14 '16 20:01 adrianbrowning

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?

dougwilson avatar Jan 14 '16 20:01 dougwilson

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

adrianbrowning avatar Jan 14 '16 21:01 adrianbrowning

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.

dougwilson avatar Jan 14 '16 21:01 dougwilson

That could work as well. :) :+1:

adrianbrowning avatar Jan 14 '16 21:01 adrianbrowning

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 avatar Jan 15 '16 01:01 dougwilson

@dougwilson Sorry never got back to you on this. Has there been any progress on adding this feature?

Thanks.

adrianbrowning avatar Jul 04 '16 13:07 adrianbrowning

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.

dougwilson avatar Jul 05 '16 18:07 dougwilson

Ah thank you @dougwilson

adrianbrowning avatar Jul 06 '16 14:07 adrianbrowning