簡體   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