javascript 在 Jade 中渲染数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25878660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:09:50  来源:igfitidea点击:

Render array in Jade

javascriptarraysnode.jspug

提问by Kien Dao

I passed an array to jade

我传递了一个数组给玉

router.get('/index', function(req, res){
    var users = ["A","B","C"];  
    res.render('index', {user: users})
});   

//what i get in jade

//我在玉中得到了什么

li #{user}
<li>a,b,c</li>                               

li #(user[0])
<li>a</li>

How can i get this instead without having to write user[0], user[1], user[2]?

我怎样才能得到这个而不需要写用户[0]、用户[1]、用户[2]?

<li>A</li> 
<li>B</li> 
<li>C</li>

I have tried following, but i had error.

我试过跟随,但我有错误。

each item in #{user}
each item in user

#{user} 中的
每个项目 user 中的每个项目

回答by Alex Wayne

Use each(see docs)

使用each(见文档)

ul
  each user in users
    li= user

And you should change your rendering call to:

您应该将渲染调用更改为:

res.render('index', {users: users})
//                       ^ added this character

Since you have an array of userobjects, you should call it users. That way you can map user, singular, to the value of each user.

由于您有一个user对象数组,您应该将其称为users。这样,您就可以将user单数映射到每个用户的价值。

回答by Josiah Ruddell

See herefor documentation.

有关文档,请参见此处

ul                     // create ul list tag
  each u in user       // start loop
    li= u              // create li tag and assign innerHTML to array item value

回答by cb6

You can also use javascript if the array is less amenable to the native each (already posted as an answer). - for (var i = 0; i < users.length; ++i) { li= users[i] - }

如果数组不太适合本机每个(已作为答案发布),您也可以使用 javascript。 - for (var i = 0; i < users.length; ++i) { li= users[i] - }

See: Loop in Jade (currently known as "Pug") template engine

请参阅:Jade 中的循环(目前称为“Pug”)模板引擎