如何使一个handlebars助手全局(在expressjs中) [英] How to make a handlebars helper global (in expressjs)
问题描述
我在 helpers / handlebars.js
中有一个非常简单的handlebars帮助文件:
I've got a pretty simple handlebars helper file in helpers/handlebars.js
:
var hbs = require('express-handlebars');
hbs.registerHelper("inc", function(value, options) {
return parseInt(value) + 1;
});
但是,如预期的那样,我不能参考 {{# inc}}
帮助器,因为我没有将它传递给 res.render()
函数。有没有办法让所有帮助者在我的文件全局和自动包含?
However, as expected, I can't refer to the {{#inc}}
helper because I didn't pass it into the res.render()
function. Is there a way to make all helpers in my file global and "auto-included"?
编辑:
在尝试@ 1cgonza的真棒答案后,我得到:
After trying @1cgonza's awesome answer, I get:
hbs.registerHelper("inc", function(value, options) {
^
TypeError: undefined is not a function
运行应用程序时,这里是 app.js
:
When running the app. Here's the 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中
You could try exporting your helpers as a module and then include them in your main app.js
这样的一个例子:
在你的助手/ handlebars.js
function hbsHelpers(hbs) {
hbs.registerHelper("inc", function(value, options) {
return parseInt(value) + 1;
});
// More helpers...
}
module.exports = hbsHelpers;
然后在您的app.js(或您用作索引的文件)中。
Then in your app.js (or the file you are using as the index).
var hbs = require('express-handlebars');
require('./helpers/handlebars')(hbs);
这是否适合您?
编辑
根据 express-handlebars
docs ,我将更改您的 helpers / handlebars.js
中的函数如下所示:
Based on the express-handlebars
docs, I would change the function in your helpers/handlebars.js
to something like this:
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;
让我们知道它是否有效。
Let us know if it works.
编辑2:
我的坏,将你的助手包裹在一个帮助者中:{}
handelbars.js
文件中的 create()
函数丢失。我已经编辑了我以前的答案,看看我在哪里发表了关于我所说的话。
My Bad, wrapping your helpers inside a helpers:{}
was missing from the create()
function in the handelbars.js
file. I've edited my previous answer, see where I placed the comment to know what I am talking about.
至于 app.js
我认为这有点混杂,所以让我重命名一些东西来说明:
As for the app.js
I think it is a little mixed up, so let me rename a few things to make it clear:
// 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');
这篇关于如何使一个handlebars助手全局(在expressjs中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!