Session: Access session data inside the template

Created on 23 Jul 2017  路  4Comments  路  Source: expressjs/session

This library should provide an easy access to session data inside the template similar to Django. I know the workaround (using res.locals) but manually putting data every time in large application really sucks.

question

Most helpful comment

@noisytoken that makes sense. Is your current setup basically doing the following?

    app.use(session(options));
    app.use(function (req, res, next) {
        res.locals.session = req.session;
        next();
    });

All 4 comments

@dougwilson thoughts on making this a configuration enabled option? My only question about that would be would this be locking us into expressjs? (I haven't been really using any of the other routers myself, do the other routers have res.locals?) This would need to be disabled by default to protect people who might be rendering templates given by users. With it enabled, it would allow users to see their entire session data.

I'd say this is outside the scope of this module, and brittle. @noisytoken what part of "manually putting data every time in large application" sucks? What is your workaround?

Recently I created a e-commerce app, in which whenever I need to show items in the cart, I have to assign session data in res.locals so than I can access them inside handlebar template. I would be great, if all the templates have direct access to session data. For instance, Django does this automatically.

@noisytoken that makes sense. Is your current setup basically doing the following?

    app.use(session(options));
    app.use(function (req, res, next) {
        res.locals.session = req.session;
        next();
    });
Was this page helpful?
0 / 5 - 0 ratings