Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Missing Helper in Handlebars.js

I am using handlebars.js templates with node and express. I am making a numbered list using the {{@index}} template tag, however since index starts at 0 and I want to start from one, it seems I need to use a custom helper. I've seen plenty of posts regarding this and I've found the following code:

Handlebars.registerHelper("inc", function(value, options)
{
    return parseInt(value) + 1;
});

{{#each score}}
      <li class="list-group-item">
      <div id="place"> {{inc @index}} &nbsp </div>
      <div class="wordOrName">{{ player_name }}</div>
           <div class="number">{{ score }}</div></li>
        {{/each}}

What I cannot seem to find is where the helper register function is supposed to go. I've tried putting it inside in the template itself and in various other places but I still keep getting

Error: Missing helper: "inc"
   at model.<anonymous>

Ideally I'd like to have the helper in a separate file helpers.js but I don't have the slightest idea of how to get handlebars to recognize it.

EDIT:

Handlebars is included in the project with the following code inside the node file index.js:

// view engine
app.set('views', __dirname + '/views/');
app.set('view engine', 'handlebars');
app.engine('handlebars', engines.handlebars); 

It appears impossible to include the helper function within the template itself.

like image 316
Cameron Sima Avatar asked Oct 10 '15 21:10

Cameron Sima


People also ask

How do I register Handlebars in Helper?

registerHelper("noop", function(options) { return options. fn(this); }); Handlebars always invokes helpers with the current context as this , so you can invoke the block with this to evaluate the block in the current context.

What are helpers in Handlebars?

Helpers can be used to implement functionality that is not part of the Handlebars language itself. A helper can be registered at runtime via Handlebars. registerHelper , for example in order to uppercase all characters of a string.

How do I compile Handlebars templates?

Getting started Next, install the Handlebars npm package, which contains the precompiler. Create a file name example. handlebars containing your template: Handlebars <b>{{doesWhat}}</b> precompiled!


1 Answers

You don't need to add require('handlebars') just to get helpers working. You can stick to express-handlebars. Define helpers in a config object like so var myConfig = { helpers: {x: function() {return "x";}} } and pass it to the express-handlebars-object like so: require('express-handlebars').create({myConfig})

Here's a fully functional example with some helpers and some view directories configured.

var express = require('express');
var exphbs = require('express-handlebars');
var app = express();
var hbs = exphbs.create({
    helpers: {
        test: function () { return "Lorem ipsum" },
        json: function (value, options) {
            return JSON.stringify(value);
        }
    },
    defaultLayout: 'main',
    partialsDir: ['views/partials/']
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));

My understanding is that the object returned from require('express-handlebars'); is not a "real" handlebars object. So you can't rely on some functions, and instead you have to pass stuff like helpers via a config object to the .create() function

like image 184
Drkawashima Avatar answered Oct 21 '22 07:10

Drkawashima