question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

handlebar helpers not available at application level

See original GitHub issue

I would like to have my handlebar helpers available to all my views, but for some reason this doesn’t work:

var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var logger = require('morgan');
var handlebars = require("express-handlebars");
var path = require('path');
var favicon = require('serve-favicon');

module.exports = function(app, envConfig){
  // view engine setup
  app.set('views', path.join(envConfig.rootPath, 'views'));

  var hbs = handlebars.create({
    helpers: {
      foo: function () { return 'FOO!'; },
      bar: function () { return 'BAR!'; }
    }
  });

  app.engine('.hbs', handlebars({
    extname: '.hbs',
    defaultLayout: 'main', 
    layoutsDir: path.join(envConfig.rootPath,'views/layouts'),
    partialsDir: path.join(envConfig.rootPath, 'views/partials')
  }));
  app.set('view engine', '.hbs');
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(cookieParser());  
   app.use(session({
    secret: 'temporary',
    resave: true,
    saveUninitialized: true
  }));
  app.use(flash());
  app.use(express.static(path.join(envConfig.rootPath, 'public')));
};

When I try to output that in my view I get nothing.

But if I set the function inside my render() method, it works:

var request = require('request');
var faker = require('faker');
var _ = require('lodash');

module.exports = {
  index: function(req, res){

      var numRecords = 10;
        var profiles = [];
        for(i=0; i < numRecords; i++) {

            profiles.push({
                full_name: faker.name.findName(),
                blurb: faker.lorem.words(5),
                avatar: faker.image.avatar()
            });
        }

    res.render('profiles/index', { 
        profiles: profiles, 
        layout : 'main',
        helpers: {
        foo: function () { return 'FOO!'; },
        bar: function () { return 'BAR!'; }
        }
     });
  }
};

Has anyone run into this issue?

Thank you in advance.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
bendersejcommented, Oct 2, 2016
app.engine('handlebars', exphbs({
  defaultLayout: 'main',
  helpers: {
    yourFirstGlobalHelper: function (foo) { return foo },
    yourSecondGlobalHelper : function (bar) { return bar}
  }
}));


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

And in your templates, simply call your global helpers this way:

<div>{{yourFirstGlobalHelper foo }}</div>

0reactions
zolitchcommented, May 12, 2017

I’ve replicated this too.

So for me when I have a an each helper: {{#each someArray}} {{>partial itemInArray}} {{/each}} Then in the partial.hbs, custom helpers that have been added to global Express are not available.

This also seems to be true for parent context items when in each ( …/parentItem) and there is a bug for this too: #196

Read more comments on GitHub >

github_iconTop Results From Across the Web

Built-in Helpers - Handlebars
Built-in Helpers. # #if. You can use the if helper to conditionally render a block. If its argument returns false , undefined ,...
Read more >
Handlebars helper not working with global variable in Express
I have an app running on Node, Express ...
Read more >
Handlebars helper reference - Celigo Help Center
Handlebars helpers can be used to implement functionality that is not part of the handlebars language itself. The following list of helpers is...
Read more >
Handlebars Helpers Reference - BigCommerce Dev Center
Differs from the get helper in that this helper will return the actual object including the given property key. This helper does not...
Read more >
handlebars - Rust - Docs.rs
ok_or(RenderError::new("param not found"))?; out.write("3rd helper: ")?; out.write(param.value().render().as_ref())?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found