1

I need to attach if condition in my handlebar template which checks the equality of string. I have registered a handlebar helper in my script file and using that within my templates. Following is my code.

Test.js file

"use strict"

const handlebars = require('handlebars');

const writeSourceFile = (filename, type) =>
    new Promise((resolve,reject) =>
        fs.writeFile(filename, type, function(err) {
            return err ? reject(err) : resolve();
        }));

handlebars.registerHelper('is_status', function(msg, matchMsg, options)
{
    if(msg === matchMsg)
        return true;
    else
        return false;
});

const tpl = handlebars.compile(fs.readFileSync('resources/my.html.hbs').toString('utf-8'));

fs.writeFileSync('/home/malintha/tracks.html', tpl(dm));

console.log("Generated source")

res.end();

..............

my.html.hbs file

{{#is_status (location this "mylocation")}}yes{{/is_status}}

I am not getting any output due to an error which is not obvious to me. My template is working fine without this custom is_status check.

What is the problem with my helper or template? Appreciate your insight.

Malintha
  • 4,512
  • 9
  • 48
  • 82
  • Could you please share the link to the source code, if possible ? It is hard to make out the context of, or replicate what you're trying to achieve, for me. – nerdier.js Jul 17 '18 at 19:08

1 Answers1

0

Rather than returning true or false in your helper, try returning options.fn(this) or options.inverse(this).

And, try calling it in your .hbs file with

{{#is_status location "mylocation"}}yes{{/is_status}}

Here is an example of this being done in the documentation. Also here is a really helpful stackoverflow answer of a more flexible way to compare variables.

Micaiah Reid
  • 439
  • 3
  • 5