V
V
Viktor Yanyshev2019-06-07 10:40:25
Node.js
Viktor Yanyshev, 2019-06-07 10:40:25

How can I change the layout depending on the route?

How to change the layout depending on the routing, do I need to change the layout from the default to the layout of the admin panel?

At the entry point, handlebars and route settings
const express = require('express'),
app = express(),
hbs = require('hbs');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
hbs.registerPartials(path.join(__dirname, 'views/partials'));

app.use('/admin', require('./routes/admin'));

Admin Routing
const express = require('express'), 
app = express(),
AdminController = require('../controllers/AdminController'),
AdminRouter = express.Router();

AdminRouter.use(function (req, res, next) {
  if(!req.session.user) {
    return res.redirect('/auth/login');
  }

  app.set("view options", {layout: "admin/layout"});
  next();
});

AdminRouter.get('/', AdminController.main);
app.set("view options", {layout: "admin/layout"}) does not work in the middleware, if I initialize the set at the entry point, then the required layout is connected, why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Yanyshev, 2019-06-07
@villiwalla

he joked, he laughed.
req, res is already app so in the middle res.local.layout = 'str';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question