I can promise you the I have yelled at my monitors by now and spend hours upon hours to understand this. First off, I can't understand the concept of having a templating engine like handlebars that cant do simple comparison functions.
In any case. I have this helper function that works great when added to the individual route as you can see here. However I would really like to add it as a global function in my app.js file created by express. I can promise you that the GITHub example of the helper function does not work.
Any help will really be appreciated.
my index.js files.
/* GET home page. */
router.get('/', function(req, res, next) {
Coupon.find(function(err, docs)
{
res.render('main/index', {
title: 'Coupon Site new for all',
coupons: docs,
//helpers
helpers: {
eq: function (v1, v2) {
return v1 === v2;
},
ne: function (v1, v2) {
return v1 !== v2;
},
lt: function (v1, v2) {
return v1 < v2;
},
gt: function (v1, v2) {
return v1 > v2;
},
lte: function (v1, v2) {
return v1 <= v2;
},
gte: function (v1, v2) {
return v1 >= v2;
},
and: function (v1, v2) {
return v1 && v2;
},
or: function (v1, v2) {
return v1 || v2;
}
},
});
});
});
And in the app.js apparently I should be able to do something like this. What is not in the documentation, I figure that I should somehow import the hbs
variable in my index.js
var hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
foo: function () { return 'FOO!'; },
bar: function () { return 'BAR!'; }
}
});
Example from ExpressHandlebars documentation:
So what you have to do to give your
gt()
,lt()
etc global scope: declare them within thehbs
(=== pass them toexphbs.create()
) variable.Passing
handlebars
property withinrender()
allows you to overwrite helpers defined withinhbs
.