Foreach loop in jade (node.js template engine)

感情迁移 提交于 2019-11-29 02:17:53

问题


Ok, I am getting an associative array from node server and trying to render it in Jade. I obviously need a foreach loop, but nothing seems to work! I tried these both codes:

- foreach row in rows {
    li= row
- }

and

- rows.forEach(function(item)) {
    li= item
- })

the array I am passing is called "rows". Any idea why this is not working? I am getting this error:

500 SyntaxError: Unexpected identifier

and, with the second code:

500 SyntaxError: Unexpected token )

回答1:


try

each item in rows
    li= item



回答2:


Your second example would work except you have a small syntax error in it - an extra parentheses, it should be:

- rows.forEach(function(item) {
  li= item
- })



回答3:


You can use

ul
  each val, index in ['zero', 'one', 'two']
    li= index + ': ' + val

or

ul
  each val, index in {1:'one',2:'two',3:'three'}
    li= index + ': ' + val

see this link



来源:https://stackoverflow.com/questions/12366402/foreach-loop-in-jade-node-js-template-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!