【问题标题】:How to install a handlebars helper with express如何使用 express 安装车把助手
【发布时间】:2016-11-01 13:40:03
【问题描述】:

我有一个使用车把的 node/express 应用程序。

我正在尝试为车把安装此帮助程序。我是用npm安装的,文件都在那里。

如何在页面上使用它?我不确定我需要在哪里包含它。我在路由和 app.js 文件中都试过了。

error: Missing helper: "svg"

这是我想使用的路线:

router.get('/app', function(req, res, next) {
  res.render('app/index', { title: 'My App', layout: false });
});

这是我要安装的助手:https://www.npmjs.com/package/handlebars-helper-svg

我发现的一切都是关于创建自己的助手,而不是加载其他人。

完全错误:

/home/ubuntu/workspace/views/project/index.hbs:缺少助手:“svg”

Error: /home/ubuntu/workspace/views/project/index.hbs: Missing helper: "svg"
    at Object.<anonymous> (/home/ubuntu/workspace/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
    at Object.eval (eval at createFunctionContext (/home/ubuntu/workspace/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:8:64)
    at main (/home/ubuntu/workspace/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
    at ret (/home/ubuntu/workspace/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
    at ret (/home/ubuntu/workspace/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
    at /home/ubuntu/workspace/node_modules/hbs/lib/hbs.js:63:19
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)

app.js 文件:http://pastebin.com/Ge6NaDXc

【问题讨论】:

  • 您没有提供任何关于如何添加车把来表达的信息。

标签: node.js express handlebars.js


【解决方案1】:
var hbs = require('express-hbs');
hbs.registerHelper('svg', require('handlebars-helper-svg'));

【讨论】:

  • 那在 app.js 中?我添加了它,它说我没有 express-hbs,所以我安装了它。但是当我尝试包含它时仍然出现错误,我包含了上面的完整消息。
  • 如果你没有 express-hbs,你是如何使用带 express 的车把的?
  • 我相信这只是我在安装 express 时设置的一个选项,我从未单独设置它。我有一条线 app.set('view engine', 'hbs');这是我的整个 app.js 文件:pastebin.com/Ge6NaDXc
  • 嘿,我终于成功了。您提供的代码有效,我还必须将东西放在正确的目录中(这是包文件夹,希望我可以改变它......)。但是感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 2020-04-02
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 2013-02-05
  • 2019-01-05
相关资源
最近更新 更多