Quick details about database
I have users table which is referenced to membership table.
Here is some sample data
userID username membershipID
1 test 1 -- Admin
2 test2 2 -- Standard
Current Handlebars
I have a handlebars page set up already
{{#if user}}
<li class="nav-item">
<a class="nav-link" href="/admin">Admin</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/logout">Logout</a>
</li>
{{else}}
<li class="nav-item">
<a class="nav-link" href="/admin">Admin</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
{{/if}}
Details of above code
When user is logged in it shows relevant nav items.
Question
I am able to get an output in handle bars of the membershipID of a user, for example:
{{user.membershipID}}
will give me an output of 1 or 2. How can I then create an if statement in handlebars to determine what content to show?
Or is there an alternative method? I can show you all my code if needed?
Any help is appreciated!
Edit 1
I am trying to test out the handlebars register:
Project/helpers/handlebars-helper.js
module.exports = {
membership: ("ifEqual", function(variable1,variable2,options){
if (variable1 === variable2) {
return options.fn(this);
} else {
return options.inverse(this);
}
})
}
project/app.js
const handlebars = require('express-handlebars');
// Public directory static file
app.use(express.static(path.join(__dirname, '/public')));
// View engine
const {membership} = require('./helpers/handlebars-helpers');
app.engine('handlebars', handlebars({defaultLayout: 'home', helpers: {membership: membership}}));
app.set('view engine', 'handlebars');
project/views/partials/home/home-nav.handlebars
{{#membership}}
{{#ifEqual 1 1}}
<h1>Test</h1>
{{/ifEqual}}
{{/membership}}
Details and output
Here I am seeing if 1 is equals to 1 and if it is then, show h1 output. But I get this error:
TypeError: Cannot read property 'inverse' of undefined
Edit 2:
Here is where I am at now
project/helpers/handlebars-helpers.js
const handlebars = require('express-handlebars');
module.exports = {
membership: handlebars.create('ifEquals', function(arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
})
}
project/views/partials/home/home-nav.handleabrs
{{#membership}}
{{#ifEqual 2 2}}
<h1>Test</h1>
{{/ifEqual}}
{{/membership}}
Output on HTML
[object Object]