在jade / keystone中生成多级列表

时间:2016-06-18 02:26:30

标签: node.js pug keystonejs

我有一个数组:

res.locals.navLinks = [
    { label: 'Main', key: 'home', href: '/' },
    { label: 'About', key: 'about', href: '/about'},
    { label: 'Contacts', key: 'contact', href: '/contact' },
    { label: 'Projects', key: 'gallery', href: '/gallery' },
    { label: 'Blog', key: 'blog', href: '/blog' },
    { label: 'Commission', key:'commission', href:'/Commission'},
    { label: 'Gov Control', key: 'govControl', href: '/govControl'}
];

需要在jade中为主菜单渲染多级列表,比如这个

ul **(first five objects)***
 li(class=(section == link.key ? 'active' : null)): a(href=link.href)= link.label
 li Partners
   ul **(last two objects)**
     li(class=(section == link.key ? 'active' : null)): a(href=link.href)= link.label

现在我只能使用代码呈现一个级别列表:

each link in navLinks
  li(class=(section == link.key ? 'active' : null)): a(href=link.href)= link.label

它可能是任何有效的解决方案,有2个阵列或另一个阵列。我m tied trying, i是编程的全新挫折。将感谢任何想法。

1 个答案:

答案 0 :(得分:0)

文字解决方案是这样的:

each link, index in navLinks
  if index < 5
    li(class=(section == link.key ? 'active' : null)): a(href=link.href)= link.label
  else if index < navLinks.length
    li
      ul
        while index < navLinks.length
          li(class=(section == navLinks[index].key ? 'active' : null)): a(href=navLinks[index].href)= navLinks[index].label
          index++

您可以考虑重组navLinks,以便嵌套列表的项目位于另一个数组中。