Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

87
Vistas
Should i put my condition in the template or render a different template

I'm learning node js and using pug-template engine to create a simple login/admin page for hotel bookings.

When a user is logged in the index page should be different from a not logged in user.

Should i do a condition in my template or a condition in my home route to render a index_logged or index_not_logged?

Currently doing

app.get('/', (req, res) => {
  User.find({}, function(err, users) {
    if(err) {
      console.log(err);
    } else {
      res.render("index", {
        title: "Willkommen",
        users: users
      });
    }
  });
});

and in my index.pug

extends layout


block content
  if user
    h1 #{title}
    ul.list-group
      each user, i in users
        li.list-group-item
          a(href="/users/"+user._id)= user.name
  else
    .jumbotron
      h1 #{title}
      a(href="/users/login").btn.btn-primary Anmelden

I'm using a global user variable at the moment.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If your public / is very different from the logged in /, then render two separate pug views, example:

app.get('/', (req, res) => {
  User.find({}, function(err, users) {
    if (err) {
      console.log(err);
      res.render("public_index");
    } else {
      res.render("index", {
        title: "Willkommen",
        users: users
      });
    }
  });
});

But if the two pages are very similar with a few certain parts different, then render the same view but use if user (theoretically you can have this in as many places as you like in the view file) to display the relevant content accordingly, like what you have in the index.pug in your question. Your app logic should be:

app.get('/', (req, res) => {
  User.find({}, function(err, users) {
    let Users = undefined;
    if (err) {
      console.log(err);
    } else {
      Users = users
    }
    res.render("index", { title: "Wilkommen", users: Users });
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda