使用变量作为jade中数组的索引

时间:2013-02-18 21:46:33

标签: express pug

我正在尝试使用变量作为索引来访问数组,然后像这样输出:

h3= users[{#id}].first_name

但是由于#{id},我得到了一个“SyntaxError:Unexpected token ILLEGAL”。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以使用id而不使用哈希或大括号。

index.js

exports.index = function(req, res){
  res.render('index', { 
    title: 'Express',
    users: [{first_name: 'John', age: 20}, {first_name: 'Mike', age: 30}],
    id: 1
  });
};

index.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  p= users[id].first_name
相关问题