Jade: Change active menu item in parent template

后端 未结 4 832
孤街浪徒
孤街浪徒 2020-12-13 02:56

I have a navigation bar in my parent jade template and I\'d like to highlight the item which is currently in view. So if I\'m on the blog page,

ul
  li Home         


        
相关标签:
4条回答
  • 2020-12-13 03:16

    Well, above solution is very clear but if someone is looking more controls over menus then there is a module available for node.js. Use this and you would have complete control over menus.

    Use Case : When menus visibles based on roles

    https://www.npmjs.com/package/active-menu

    0 讨论(0)
  • 2020-12-13 03:18

    parent.jade

    doctype 5
    
    html
      block link
        -var selected = 'home'; //default
    
      -var menu = { 'home': '/home', 'blog': '/blog', 'contact': '/contact' };
    
      body
        nav
          ul
            each val, key in menu
              li
                if selected === key
                  a.selected(href=val, title=key)= key
                else
                  a(href=val, title=key)= key
    

    child.jade

    extends parent
    
    block link
      -var selected = 'blog';
    
    0 讨论(0)
  • Use the Current object with a ternary expression. All is in the documentation.

    You can use the object properties to generate your active menu like so. If you want to use folders in your navigation menu (Jade version) :

    ul(class="nav-menu")
                li: a(class="#{ current.path[0] === 'index' ? 'active' : '' }", href="/") home
                li: a(class="#{ current.path[0] === 'about' ? 'active' : '' }", href="/about/") about

    0 讨论(0)
  • 2020-12-13 03:25

    Here's a simpler way:

    Use this in your layout.jade (where nav is the name of the page that's active. nav = 'about' for example)

    ul(class="#{nav}")
      li.home Home
      li.blog Blog
      li.contact Contact Us
      li.about About
    

    Then put this is your CSS:

    ul.home li.home,
    ul.blog li.blog,
    ul.contact li.contact,
    ul.about li.about {
        color: red;
    }
    

    The only css rule that will apply is the one whose ul class exists. You'll need to pass in a variable nav that equals 'about', 'home', 'contact', or 'blog' depending on what page you're on.

    0 讨论(0)
提交回复
热议问题