在 express 中使用 pug(jade) 创建 table 并添加数据
creating table and adding data using pug(jade) in express
我正在用玉创建一个 table 快递。我无法在 table 中输入值。我正在处理一条产品路线。
index.js
var express = require('express'),
path = require('path'),
home = require('./routes/home.js');
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use('/home', home);
app.listen(3000);
console.log('Access through http://localhost:3000/');
home.js
var express = require('express');
var router = express.Router();
router.get('/home', function(req, res) {
res.render('home/index', { id:'1',name:'hp', quantity:'3'},{ id:'2',name:'dell', quantity:'2'},{ id:'3',name:'sony', quantity:'5'});
});
module.exports = router;
index.jade
doctype
html
head
title=title
body
h3 Comments
table
tr
th Id
th Name
th Quantity
each item in item
tr
td #{item.id}
td #{item.name}
td #{item.quantity}
您应该在单个对象中将局部变量传递给 Pug。将项目对象写成数组将有助于迭代它。在 home.js
:
中试试这个
res.render('home/index', {
items: [
{ id:'1', name:'hp', quantity:'3' },
{ id:'2', name:'dell', quantity:'2' },
{ id:'3', name:'sony', quantity:'5' }
]
});
然后在你的 Pug 中,为你的 each
循环中的两个项目使用不同的变量名很重要("item" vs "items"):
each item in items
tr
td #{item.id}
td #{item.name}
td #{item.quantity}
我正在用玉创建一个 table 快递。我无法在 table 中输入值。我正在处理一条产品路线。
index.js
var express = require('express'),
path = require('path'),
home = require('./routes/home.js');
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use('/home', home);
app.listen(3000);
console.log('Access through http://localhost:3000/');
home.js
var express = require('express');
var router = express.Router();
router.get('/home', function(req, res) {
res.render('home/index', { id:'1',name:'hp', quantity:'3'},{ id:'2',name:'dell', quantity:'2'},{ id:'3',name:'sony', quantity:'5'});
});
module.exports = router;
index.jade
doctype
html
head
title=title
body
h3 Comments
table
tr
th Id
th Name
th Quantity
each item in item
tr
td #{item.id}
td #{item.name}
td #{item.quantity}
您应该在单个对象中将局部变量传递给 Pug。将项目对象写成数组将有助于迭代它。在 home.js
:
res.render('home/index', {
items: [
{ id:'1', name:'hp', quantity:'3' },
{ id:'2', name:'dell', quantity:'2' },
{ id:'3', name:'sony', quantity:'5' }
]
});
然后在你的 Pug 中,为你的 each
循环中的两个项目使用不同的变量名很重要("item" vs "items"):
each item in items
tr
td #{item.id}
td #{item.name}
td #{item.quantity}