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

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

    You can pass the pageName in the data.

    each menu in ["Home", "Dashboard", "Contact", "About"]
        if (menu == pageName)
          li.active
            a(href="/#{menu}")= menu
        else
          li
            a(href="/#{menu}")= menu
    

    You can call render

    res.render("index", { pageName: 'Home'})
    
    res.render("index", { pageName: 'Dashboard'})
    
    res.render("index", { pageName: 'Contact'})
    

提交回复
热议问题