繁体   English   中英

如何使把手助手成为全球性的(在 expressjs 中)

[英]How to make a handlebars helper global (in expressjs)

我在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');

有任何想法吗?

您可以尝试将您的助手导出为模块,然后将它们包含在您的主 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文档,我会将您的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:

我的handelbars.js文件中的create()函数缺少将您的助手包装在helpers:{}中。 我已经编辑了我之前的答案,请查看我发表评论的位置以了解我在说什么。

至于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');

你可以试试:

在 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');

这是我的解决方案。 您可以注册您自己的客户助手和来自 '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');

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

在你的 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");

通过注册像这样的部分,可以在整个车把中使用它作为内置功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM