I am trying to implement Handlebars templating logic to show a "logged in" navbar (with a user's first name and profile image) if the user is authenticated or a "logged out" navbar if the user is not authenticated.
To do this, I am using the handlebars.js conditional statement:
{{#if login_status}}
*logged in navbar html code*
{{else}}
*logged out navbar html code*
{{/if}}
I am also using a helper login_status.js that returns true if authenticated and false is not authenticated, based on the existence of a user's ID. My helper code looks like this:
module.exports = function(l) {
if (l.data.root.id === undefined) {
console.log('user is logged out');
return false;
} else {
console.log('user is logged in: ', l.data.root.id);
return true;
}
};
However, this does not work, and it is always returning the {{else}} statement, meaning that the user is not authenticated.
Is there a better way of implementing this logic? Is the helper code incorrect? Is the handlebars {{#if}} expression incorrect?
Any help is appreciated.
Thanks!
Jesse
phkavitha trying to say is that you can use (login_status function) as a property(getter) instead of a helper.
you can say:
var template = Handlebars.compile([your handlebars template]);
var html = template({
...
get login_status() {
return require('login_status.js')(l);
},
...
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.