expressswig-template

Pass request to every Swig template using Express


Is it possible to pass request/req object to every template I render, so I wouldn't have to pass it as local in every render() method?


Solution

  • You can use res.locals for that.

    Insert the following middleware somewhere before your routes:

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

    This exposes req as a variable in your templates.