如何使把手帮助器全局化(在快递中)

时间:2015-09-22 02:10:35

标签: javascript express handlebars.js helper

我在helpers/handlebars.js中有一个非常简单的把手助手文件:

var hbs = require('express-handlebars');

hbs.registerHelper("inc", function(value, options) {
    return parseInt(value) + 1;
});

但是,正如预期的那样,我无法引用{{#inc}}助手,因为我没有将其传递到res.render()函数中。有没有办法让我的文件中的所有助手都是全局的"自动包含"?

修改

在尝试了@ 1cgonza的精彩答案之后,我得到了:

hbs.registerHelper("inc", function(value, options) {
      ^
TypeError: undefined is not a function

运行应用时。这是app.js

var engine      = require('express-handlebars');
                  require('./helpers/handlebars.js')(engine);

app.engine('hbs',           engine({defaultLayout: 'layout', extname: 'hbs'}));
app.set('view engine',      'hbs');

有什么想法吗?

4 个答案:

答案 0 :(得分:16)

您可以尝试将助手导出为模块,然后将其包含在主app.js

这样的事情:

helpers/handlebars.js

function hbsHelpers(hbs) {
  hbs.registerHelper("inc", function(value, options) {
    return parseInt(value) + 1;
  });

  // More helpers...
}

module.exports = hbsHelpers;

然后在你的app.js(或你用作索引的文件)。

var hbs = require('express-handlebars');
require('./helpers/handlebars')(hbs);

这对你有用吗?

修改

根据express-handlebars docs,我会将helpers/handlebars.js中的函数更改为以下内容:

function hbsHelpers(hbs) {
  return hbs.create({
    helpers: { // This was missing
      inc: function(value, options) {
        console.log('reading it');
        return parseInt(value) + 1;
      }

      // More helpers...
    }

  });
}

module.exports = hbsHelpers;

告诉我们是否有效。

编辑2:

我的错误,helpers:{}文件中的create()函数中缺少将您的助手包裹在handelbars.js内。我已经编辑了我以前的答案,看看我在哪里发表评论,知道我在说什么。

关于app.js我觉得它有点混乱,所以让我重命名一些事情来说清楚:

// I've changed this from engine to exphbs,
// so there is no confusion with the express engine object that we use later.
var exphbs = require('express-handlebars');

// Create an instance of the express-handlebars
// If you want to pass any option offered by express-handlebar module
// do it inside the create() in the handlebars.js file
var handlebars  = require('./helpers/handlebars.js')(exphbs);

// The handlebars variable now has an object called engine.
// Use that to define your app.engine
// As said before, you don't need to define any options here.
// Everything is defined in the create() in handlebars.js
app.engine('hbs', handlebars.engine);

// If you are using a different extension, you can change hbs to whatever you are using. 
app.set('view engine', 'hbs');

答案 1 :(得分:9)

你可以尝试:

in helpers / handlebars.js:

var register = function(Handlebars) {
    var helpers = {
    inc: function(value, options) {
        return parseInt(value) + 1;
    },
    foo: function(var1, var2) {
        return ....
    }
};

if (Handlebars && typeof Handlebars.registerHelper === "function") {
    for (var prop in helpers) {
        Handlebars.registerHelper(prop, helpers[prop]);
    }
} else {
    return helpers;
}

};

module.exports.register = register;
module.exports.helpers = register(null); 
app.js中的

var exphbs = require('express-handlebars');
var hbsHelpers = exphbs.create({
    helpers: require("./helpers/handlebars.js").helpers,
    defaultLayout: 'layout',
    extname: '.hbs'
});

app.engine('.hbs', hbsHelpers.engine);
app.set('view engine', '.hbs');

答案 2 :(得分:0)

这是我的解决方案。您既可以注册自己的客户助手,也可以从“ handlebars-helpers”中注册客户助手:

const hbshelpers = require('handlebars-helpers');
const multihelpers = hbshelpers();

const helpersDM = {
    hlp: echo => `Echo: ${echo}.`,
    STATIC: `/static`,
};
const hbs = exphbs.create({
    layoutsDir: join(__dirname, 'views', 'layouts'),
    partialsDir: join(__dirname, 'views', 'partials'),
    extname: '.hbs',
    defaultLayout: 'base',
    helpers: {...multihelpers, ...helpersDM},
});
app.engine('.hbs', hbs.engine);
app.setViewEngine('.hbs');

答案 3 :(得分:0)

您可以使用以下方法注册自己的助手,

在您的app.js中,首先导入express-handlebars,express npm模块

const exphbs = require("express-handlebars");
const express = require("express");
const app = express();

现在,使用将选项作为对象的create方法配置exphbs,

const myhbs = exphbs.create({/*config*/ extname:"hbs", helpers:{equal:function(a,b, options){return (a==b)?options.fn(this):options.inverse(this)}}});
app.engine("hbs", myhbs.engine);
app.set("view engine", "hbs");

通过像这样注册部分,可以在整个车把中将其用作内置功能。<​​/ p>