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!! ^^