Search code examples
javascriptif-statementpug

Condition if else in pug


I would like to make a simple condition in pug, which is : If this element exists > Show this element / Else > Show a text

There is my code used :

In app.js :

  axios
    .get(`${process.env.API_URL}/party/${req.params.id}`)
    .then(({ data }) => {
      let items = null;
      data.items.length === 0 ? items = false : items = true;
      res.render('party', { 
        party: data,
        title: data.name,
        items,
        url: `${process.env.FRONT_URL}:${process.env.PORT}/party/${data._id}` 
    })})
    .catch((err) => console.log(err))
  ;
});

In my .pug file :

if items = false
  each item in party.items
    form(method="post" action=`/party/${party._id}/items/${item._id}`)
      p= `${item.name} - ${item.user}`
      button(type="submit") Supprimer
else 
  p Il n'y a pas encore d'objet. Ajoutez-en un !

What should I write after my if?


Solution

  • So I got helped and there is the answer:

    each item in party.items
      form(method="post" action=`/party/${party._id}/items/${item._id}`)
        p= `${item.name} - ${item.user}`
        button(type="submit") Supprimer
    if party.items.length === 0 
      p Il n'y a pas encore d'objet. Ajoutez-en un !