Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
use helper of handlebars-express in a view

I am using nodejs as a backend and the handlebars-express library

I have 3 files, my index.js, loginController.js and loginTemplate.handlebars

In my index.js I have declared handlebars like this

const handlebarsInstance = require('./src/services/handlebarsService');
var hbs = handlebarsInstance.handlebars;
const server = express();
    
server.engine('handlebars', hbs.engine);
server.set('view engine', 'handlebars');

In my loginController I have this

const handlebarsInstance = require('../services/handlebarsService');

      handlebarsInstance
  .getTemplate('loginController/loginTemplate', data)
  .then((loginCompiledTemplate) => {
    res.send(loginCompiledTemplate);
  });

My handlebars service:

const handlebars = require('express-handlebars').create({
  helpers: {
    ifCond: function (v1, operator, v2, options) {
      switch (operator) {
        case '==':
          return v1 == v2 ? true : false;
        case '===':
          return v1 === v2 ? true : false;
        case '!=':
          return v1 != v2 ? true : false;
        case '!==':
          return v1 !== v2 ? true : false;
        case '<':
          return v1 < v2 ? true : false;
        case '<=':
          return v1 <= v2 ? true : false;
        case '>':
          return v1 > v2 ? true : false;
        case '>=':
          return v1 >= v2 ? true : false;
        case '&&':
          return v1 && v2 ? true : false;
        case '||':
          return v1 || v2 ? true : false;
        default:
          return false;
      }
    },
  },
  layoutsDir: __dirname + '/src/views',
});

const getTemplate = async (path, data) => {
  const template = await handlebars
    .getTemplate(`./src/views/${path}.handlebars`)
    .catch((err) => {
      console.log(err);
    });
  return template(data);
};

module.exports = { getTemplate, handlebars };

But when in my view I try to call "ifCond" it doesn't recognize it, because it gives me this error

{{#if campos }}
{{#each camposManuales}}
{{#ifCond this.field_type == 1 }}
  a
{{/ifCond}}
{{/each}}
{{/if}}

The error:

UnhandledPromiseRejectionWarning: Error: Parse error on line 14:
           .field_type == 1 }}

How can i fix this? What is wrong? Thank u so much!! ^^

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!