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

前端 未结 7 825
刺人心
刺人心 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:53

    This has a little less duplication.

    ul.nav
      each name in ["Dashboard", "About", "Contact"]
        if (menu=="Home")
          li.active
            a(href="#")= name
        else
          li
            a(href="#")= name
    

    Adding the active class might be better just done in javascript or perhaps you can get the presentation you need with the straight CSS a:active selector.

提交回复
热议问题