node.js + jade + express: How can I create a navigation that will set class active if the path matches

前端 未结 7 822
刺人心
刺人心 2021-01-31 06:31

I have come up with the following code but the problem is, there will be duplication of the anchor tag for each menu item.Is there a better way to do this?

              


        
7条回答
  •  情歌与酒
    2021-01-31 06:42

    Very flexible without duplication:

    ul
      each menuitem in [{title:'Home',url:'/'},{title:'About',url:'/about'},{title:'Contact',url:'/contact'}]
        if (menuitem.title == title)
          li.active
            a.active(href=menuitem.url) #{menuitem.title}
        else
          li
            a(href=menuitem.url) #{menuitem.title}
    

    You need to set the page's current title.

提交回复
热议问题