从Express/node.js Web应用程序预填充HTML/Jade [英] Pre-populating HTML/Jade from Express/node.js web app
问题描述
我有一个使用Express for node.js构建的Web应用程序.我正在将Jade模板文件用于HTML显示.在这些显示之一中,我想用数据预先填充各个字段.数据存储在mongodb会话存储中,也存储在db中的单独集合中.我更喜欢使用会话数据在HTML/Jade显示中预填充这些字段.我该怎么做(如果可能的话)?
I have a web app that is built using Express for node.js. I'm using Jade template files for the HTML displays. In one of these displays I'd like to have the various fields pre-populated with data. The data is being stored in a mongodb session store, as well as in a separate collection in the db. I'd prefer to use the session data to pre-populate these fields in the HTML/Jade displays. How can I go about doing this (if it's possible)?
推荐答案
将默认值添加到res.locals
,然后在玉中设置input
元素value
属性.
Add the defaults to res.locals
and then set the input
elements value
attribute in jade.
//node.js
app.get('/', function(req, res){
// Sorry I am unfamiliar with Mongo, not sure the syntax...
mongo.get('defaults', function(err, body){
res.locals.dName = body.defaultName;
res.locals.dFoo = body.defaultFoo;
res.render('myTemplate');
});
});
//myTemplate.jade
!!!
html
body
form(action='/form', method='post')
input#formName(name='name', value=locals.dName)
input#formFoo(name='foo', value=locals.dFoo)
这篇关于从Express/node.js Web应用程序预填充HTML/Jade的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!