express-handlebars icon indicating copy to clipboard operation
express-handlebars copied to clipboard

Calling helper inside another helper - Question

Open boulepick opened this issue 7 years ago • 0 comments

Hello, can you let me know if it is possible to call a helper inside another helper. i need to call an i18n helper inside a date format helper. i cannot seem to get it to work. was wondering if that is even possible with this plugin.

here is my setup:

const express = require('express'); const i18next = require('i18next');

i18next.init(options);

app.use(i18nextMiddleware.handle(i18next));
app.post(path.join(__dirname,'/locales/add/{{ns}}/{{lng}}'), i18nextMiddleware.missingKeyHandler(i18next));
app.get(path.join(__dirname,'/locales/resources.json'), i18nextMiddleware.getResourcesHandler(i18next));

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.engine('hbs', exphbs({
	extname: 'hbs',
	defaultLayout: 'main_layout',
	layoutDir: path.join(__dirname, 'views/layouts/'),
	partialsDir: path.join(__dirname,'views/partials/'),
	helpers: {
		i18n: function(key, options){
			var result = this.t(key, options.hash);
			return new Handlebars.SafeString(result);
		},
		formatExpire: function (namespace, date, format, lang) {
  		// strText = "expired On";
  		// strText = Handlebars.i18n.t('namespace:translationString'); //  Cannot read property 't' of undefined
  		// strText = Handlebars.i18n('namespace:translationString'); // Handlebars.i18n is not a function
  	       strText = Handlebars._default_helpers['i18n'].t('namespace:translationString'); // t is not a function
  					
  		htmlDisplay = "<span class='bg-danger-800 text-highlight'>" + strText + "<span class='text-semibold'></span></span>";
  					
  		return htmlDisplay;
		}
		...
		....
    }
    })
  )

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

anything else works great, all of the helpers i have no issues with. only when i call the "formatExpire" that i get the errors listed as comments in the helper function.

Any help would be greatly appreciated.

thanks

boulepick avatar Oct 16 '17 16:10 boulepick