在车把 + express 上与助手一起工作时遇到麻烦
Trouble working with helper on handlebars + express
我正在尝试使用助手,但出现此错误:ReferenceError: a is not defined
我正在尝试在视图目录下显示 home.hbs。因为它包含如下内容:
<li class="{{#if_eq title "Home"}}active{{/if_eq}}">
<a href="/">Home</a>
</li>
这是app.js
const hbs = require('express-handlebars');
// Handlebars configuration
app.engine('hbs', hbs({
extname: 'hbs',
layoutsDir: path.join(__dirname, '/views/layouts/'),
partialsDir: path.join(__dirname, '/views/partials/'),
helpers: {
if_eq: function () {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
},
getStringifiedJson: function (value) {
return JSON.stringify(value);
}
}
}));
app.set('view engine', 'hbs');
我尝试使用 handlebars 文档,但 NPM 上似乎有很多版本,例如 hbs、express-handlebars、exphbs。我有点困惑。
函数中没有参数。因此错误。这应该有效
if_eq: function (a, b, options) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
}
我正在尝试使用助手,但出现此错误:ReferenceError: a is not defined
我正在尝试在视图目录下显示 home.hbs。因为它包含如下内容:
<li class="{{#if_eq title "Home"}}active{{/if_eq}}">
<a href="/">Home</a>
</li>
这是app.js
const hbs = require('express-handlebars');
// Handlebars configuration
app.engine('hbs', hbs({
extname: 'hbs',
layoutsDir: path.join(__dirname, '/views/layouts/'),
partialsDir: path.join(__dirname, '/views/partials/'),
helpers: {
if_eq: function () {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
},
getStringifiedJson: function (value) {
return JSON.stringify(value);
}
}
}));
app.set('view engine', 'hbs');
我尝试使用 handlebars 文档,但 NPM 上似乎有很多版本,例如 hbs、express-handlebars、exphbs。我有点困惑。
函数中没有参数。因此错误。这应该有效
if_eq: function (a, b, options) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
}