[英]How to set path to partials in Node.js with Express, Handlebars and Conslidate
[英]How to use register helpers for node.js and express handlebars
我的表单上有一个选择选项,在加载记录后,我想选择保存的选项。 这是代码:
我正在显示表单的Student.hbs文件,行为obj来自student.js路由
student.hbs
<form>
<div class="form-group">
<label for="day">Day</label>
<select name="day" id="day" class="form-control">
<option value="monday" {{{select act.day 'monday'}}}>Monday</option>
<option value="tuesday" {{{select act.day 'tuesday'}}}>Tuesday</option>
<option value="wednesday" {{{select act.day 'wednesday'}}}>Wednesday</option>
<option value="thursday" {{{select act.day 'thursday'}}}>Thursday</option>
<option value="friday" {{{select act.day 'friday'}}}>Friday</option>
<option value="saturday" {{{select act.day 'saturday'}}}>Saturday</option>
<option value="sunday" {{{select act.day 'sunday'}}}>Sunday</option>
</select>
</div>
</form>
student.js
router.get('/view/:actUUID', (req, res) => {
var uuid = req.params.actUUID;
Student.findByPk(uuid).then(act => {
res.render('student', {
act: act
});
});
});
我已经在/handlers/handlebars.js中创建了车把帮助器,并在其中编写了所有帮助器函数。
app.js
var exphbs = require('express-handlebars');
var hbsHelpers = exphbs.create({
helpers:require('./handlers/handlebars').helpers,
defaultLayout: 'main',
extname:'.hbs'
});
app.engine('.hbs', hbsHelpers.engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
/handlers/handlebars.js
function hbsHelpers(hbs) {
return hbs.create({
helpers: {
select: function (selected, option) {
return (selected == option) ? 'selected="selected"' : '';
}
}
});
}
module.exports = hbsHelpers;
但是当我转到学生页面时,出现以下错误
错误:缺少帮助程序:在对象处“选择”。 (/ Volumes / Macintosh HD 2 / GitHub已克隆Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / helpers / helper-missing.js:19:13),位于Object.eval [作为主要对象](在createFunctionContext中为eval) (/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / compiler / javascript-compiler.js:254:23),: 15:74)位于主目录(/ Volumes / Macintosh HD 2 / GitHub已克隆Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / runtime.js:175:32)在ret(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / ret(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / compiler / compiler.js上的handlebars / dist / cjs / handlebars / runtime.js:178:12): 526:21)在ExpressHandlebars._renderTemplate(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / express-handlebars / lib / express-handlebars.js:247:12)中。 (/卷/ Macintosh HD 2 / GitHub克隆的Repos / action-tours / node_modules / express-handlebars / lib / express-handlebars.js:173:21)
因此,似乎没有必要在handlebars.js文件中返回hbs.create。
正确的方法是:
app.js
var exphbs = require('express-handlebars’);
.
.
.
var hbs = exphbs.create({
helpers: require('./handlers/handlebars'),
defaultLayout: 'main',
extname:'.hbs'
});
.
.
app.engine('.hbs', hbs.engine);
在/handlers/handlebars.js中
module.exports = {
select: function (selected, option) {
return (selected == option) ? 'selected="selected"' : '';
}
};
在/handlers/handlebars.js中
你做这样的事情
module.exports = {
select: function(selected, options){
return options.fn(this).replace( new RegExp(' value=\"' + selected + '\"'), '$& selected="selected"').replace( new RegExp('>' + selected + '</option>'), ' selected="selected"$&');
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.