【问题标题】:Handlebars is saying it's missing the helper?Handlebars 说它缺少助手?
【发布时间】:2018-02-14 00:04:46
【问题描述】:

我正在尝试从 express 手动渲染车把模板。

//get handlebars
var hbs = require('hbs').create({
  svg: require('handlebars-helper-svg'),
  switch: require('../helpers/switch.js'),
  case: require('../helpers/case.js'),
});

console.log(hbs)

//convert each entry to HTML via handlebars
var resultsHtml = results.map(function (tutorial) {
  var template = require('../views/list-item.hbs');
  template = hbs.compile(template);

  return template(tutorial);
});

hbs 的日志给出了这个:

Instance {
  handlebars: 
   { svg: { [Function] cache: {} },
     switch: [Function],
     case: [Function] },
  cache: {},
  __express: [Function: bound middleware],
  SafeString: undefined,
  Utils: undefined }

这似乎有 3 个助手,但它不断吐出错误

Error: Missing helper: "svg"

除非我从模板中删除所有 SVG。

我在 app.js 中的主应用程序中使用了这个帮助程序,如下所示:

var svg = require('handlebars-helper-svg');
hbs.registerHelper('svg', svg);

但这在我的路线中不起作用,无论如何。

我做错了什么?

【问题讨论】:

    标签: express handlebars.js


    【解决方案1】:

    问题不在于助手,而在于模板。 Require 用于 javascript 文件,并且必须在其中调用 modules.export。对于其他文件,您需要使用文件系统对象来读取文件:

    fs.readFileSync('./views/list-item.hbs', 'utf8')
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多