【问题标题】:Trouble working with helper on handlebars + express与车把上的助手一起工作时遇到麻烦+快递
【发布时间】:2018-10-16 11:50:47
【问题描述】:

我正在尝试使用帮助程序,但出现此错误: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');

我正在尝试使用车把文档,但 NPM 上似乎有很多版本,比如 hbs、express-handlebars、exphbs。我有点困惑。

【问题讨论】:

    标签: javascript node.js express handlebars.js


    【解决方案1】:

    函数中没有参数。因此错误。这应该工作

    if_eq: function (a, b, options) {
          if (a == b) {
            return opts.fn(this);
          } else {
            return opts.inverse(this);
          }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2014-12-19
      • 2020-04-02
      相关资源
      最近更新 更多