简体   繁体   中英

NodeJS /Express/ ExpressHandlebars - global helper functions

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 :

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

var app = express();

var hbs = exphbs.create({
    // Specify helpers which are only registered on this instance. 
    helpers: {
        foo: function () { return 'FOO!'; },
        bar: function () { return 'BAR!'; }
    }
});

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

app.get('/', function (req, res, next) {
    res.render('home', {
        showTitle: true,

        // Override `foo` helper only for this rendering. 
        helpers: {
            foo: function () { return 'foo.'; }
        }
    });
});

app.listen(3000);

So what you have to do to give your gt() , lt() etc global scope: declare them within the hbs (=== pass them to exphbs.create() ) variable.

Passing handlebars property within render() allows you to overwrite helpers defined within hbs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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