如何在玉器中进行嵌套迭代? [英] How to do nested iterations in jade?
问题描述
ul
li
a(href='') menu1
li
a(href='') menu2
ul
li
a(href='') sub-menu2
li
a(href='') menu3
ul
li
a(href='') sub-menu3
ul
li
a(href='') secondary-submenu3
这就是我想要实现的,而不是写上面的代码,我希望能够使用循环来生成它。
我尝试了jade文档,这些例子只显示了一个循环级别。例如。我可以试试这个
This is what I'm trying to achieve, instead of writing the above jade code I want to be able to generate it using loops. I tried the jade documentation, the examples show only one level of loop. For eg. I could try this
-var menus = [ 'menu1', 'menu2', 'menu3' ];
ul
each menu in menus
li
a(href='') #{menu}
但这还不够。如果我尝试这样的话,那么
But this is not enough. if I try this
-var submenus = [ 'sub-menu1', 'sub-menu2', 'sub-menu3' ];
ul
each menu in menus
li
a(href='') #{menu}
ul
each submenu in submenus
li
a(href='') #{submenu}
如果每个菜单项有相同数量的子菜单项。
但在我的情况下,每个菜单项的子菜单项数量都不相同。
我该如何解决这个问题?
如何在jade中做嵌套迭代?
this might work if each menu item had equal number of submenu items. But in my case number of submenu item differs for each menu item. How do i go around this? How to do nested iterations in jade?
推荐答案
你需要一个这个结构的对象,例如:
you need a object for this structure, for instance:
当地居民:
{
menus: {
m1: ['a', 'b', 'c', 'd'],
m2: ['x', 'y', 'z'],
m3: ['i', 'ii']
}
}
然后你可以使用这个template:
then you can use this template:
ul
each menuKey in Object.keys(menus)
- menu = menus[menuKey]
li
a(href='')=menuKey
ul
each submenu in menu
li
a(href='') #{submenu}
你可以在这个网站上试试这个: http://jade-lang.com/demo/
将模板复制粘贴到左上方,并将当地人粘贴到右上方的textarea。
you can try this on this site: http://jade-lang.com/demo/ copy paste the template to the top left, and the locals to the top right textarea.
这篇关于如何在玉器中进行嵌套迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!