Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide method to add to template locals #42

Open
adrianbrowning opened this issue Jan 14, 2016 · 8 comments
Open

Provide method to add to template locals #42

adrianbrowning opened this issue Jan 14, 2016 · 8 comments

Comments

@adrianbrowning
Copy link

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);
@dougwilson dougwilson self-assigned this Jan 14, 2016
@dougwilson
Copy link
Contributor

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?

@adrianbrowning
Copy link
Author

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

@dougwilson
Copy link
Contributor

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.

@adrianbrowning
Copy link
Author

That could work as well. :) 👍

@dougwilson
Copy link
Contributor

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 dougwilson changed the title Template callback function, having the request, and response sent through as well Provide method to add to template locals Jan 15, 2016
@adrianbrowning
Copy link
Author

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

Thanks.

@dougwilson
Copy link
Contributor

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.

@adrianbrowning
Copy link
Author

Ah thank you @dougwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants