0

I'm making a site with handlebars and nodejs and I want to show a "message" (you can best compare them to tweets) only if the "message" is from a specific user.

For the if statement I used the following question asked on stackoverflow in the past: Logical operator in a handlebars.js {{#if}} conditional I'm passing through 2 variables: messages and username where username is a string and messages an array. I use message as |item| to get a specific place from the array. The username is stored in item.[0]. As I change the username in the value to "test" in line 3 of the hbs code so it would be {{#ifCond item.[0] "test"}} then it works, but not with the username passed through with the value.

Here's my handlebars code:

        {{#if user}}
          {{#each messages as |item|}}
            {{#ifCond item.[0] username}} 
              <div class="card" style="width: 100%;">
              <div class="card-body">
                <h5 class="cardtitle">{{@key}}</h5>
                <h6 class="card-subtitle mb-2 text-muted"><a href="/users?user={{item.[0]}}">{{item.[0]}}</a></h6>
                <h6 class="card-subtitle mb-2 text-muted">{{item.[2]}}</h6>
                <p class="card-text">{{item.[1]}}</p>
              </div>
              </div>
            <p></p>
            {{else}}
              <p>{{item.[0]}}</p>
            {{/ifCond}}
        {{/each}}
      {{/if}}

The node.js code to load the page:

router.get('/users',loggedIn,messages,(req, res) => {
    var user = res.user
    var username = req.query.user
    var message = res.message  
    if (user != undefined){
        res.render("publicprofile", {user:user, username:username, messages:message, usernamestr:String(username)})
    } else{
        res.redirect("/")
    }
});

and the handlebars helper:

hbs.registerHelper('ifCond', function(v1, v2, options) {
    if(v1 === v2) {
      return options.fn(this);
    }
    return options.inverse(this);
  });
Tijmi
  • 45
  • 8
  • Which username are you changing to "test" and in what way does it make it "work"? – 76484 Jan 23 '23 at 16:47
  • in line 3 of the handle bars so it would be `{{#ifCond item.[0] "test} ` – Tijmi Jan 24 '23 at 06:24
  • Well this suggests that each message item has a `0` element of "test". Is this correct? It might be helpful if you post what the `messages` value is. – 76484 Jan 24 '23 at 11:50
  • no this was just a user named test on wich profile I was testing, this will be diffirent by other users – Tijmi Jan 24 '23 at 13:58
  • I would begin by logging the `username` from the template to see what value it holds. I would add `{{log username}}` right before the `#ifCond` line. – 76484 Jan 24 '23 at 14:06
  • it logs the username in my case test – Tijmi Jan 24 '23 at 14:16
  • I am not fully following what the issue is. One thing I do know is that the `username` in the line `{{#ifCond item.[0] username}}` is _not_ in context, and so it should be `undefined`. What I think you want is `{{#ifCond item.[0] ../username}}`. – 76484 Jan 24 '23 at 14:40
  • 1. so it has to be the same to go ythough the condition wich it is but it doesn' t go through 2. this works thanks – Tijmi Jan 24 '23 at 14:50
  • Does this answer your question? [Access properties of the parent with a Handlebars 'each' loop](https://stackoverflow.com/questions/12297959/access-properties-of-the-parent-with-a-handlebars-each-loop) – 76484 Jan 24 '23 at 16:16
  • it already worked with the ` {{#ifCond item.[0] ../username}}` thanks for helping – Tijmi Jan 25 '23 at 09:17

0 Answers0