简体   繁体   中英

Render array in Jade

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]?

<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

Use each (see docs)

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 user objects, you should call it users . That way you can map user , singular, to the value of each user.

See here for documentation.

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

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] - }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM