Toggle active link Bootstrap navbar

若如初见. 提交于 2019-12-03 20:11:42
brnrd

You can try with something like that in you jade layout :

li(class=title=='Home'?'active':undefined)
  a(href="/") Home
li(class=title=='About'?'active':undefined)
  a(href="/about") About
li(class=title=='Contact'?'active':undefined)
  a(href="/contact") Contact

Then just add this to your server.js :

app.get('/', function (req, res) {
  res.render('index', title: 'Home')
});

app.get('/about', function (req, res) {
  res.render('about', title: 'About')
});

This way, you can also remove the hard coded title in your layout and modify it through this solution as :

title #{title} | Test Application

And it will render this as a title for your home :

Home | Test Application

Here the solution I've found (I dont have the link anymore, sorry for the code owner)

ul.nav
    -var obj = { 'home':'Home', 'about':'About', 'contact':'Contact' }
    -each val, key in obj
         -if (id == key)
             li.active
               a(href='#{key}') #{val}
         -else            
             li
               a(href='#{key}') #{val}

and I set the server.js as

app.get('/about', function (req, res) {
    res.render("about", {
        title: 'About', 
        id: 'about',
        user: JSON.stringify(req.user, 0, 2)
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!