Comparison in an IF block

I’m attempting to mutate a string in place before saving a device state attribute.

What I’m trying to accomplish in plain english is:
If the string {{ABC}} is ’string1’ or ’ string2’, then return a ‘blank’, otherwise return the string ABC.

I’ve built a handlebar using the following statement:
{{#if ({{ABC}} === ‘(empty)’) || ({{ABC}} === ‘(scroll lock symbol)’)}}{{‘blank’}}{{else}}{{ABC}}{{/if}}

it keeps saying it’s an invalid template, and I have no doubt that’s the case but i can’t quite see my mistake. I maybe become lost in the braces or brackets somewhere :slight_smile:

Here’s an expression that does what you want:

{{#eq data.value 'string1'}}blank{{else}}{{#eq data.value 'string2'}}blank{{else}}{{data.value}}{{/eq}}{{/eq}}

If you were to look at that as code, it would look like this:

if(data.value === 'string1') { // {{#eq data.value 'string1'}}
  return 'blank';
}
else { // {{else}}
  if(data.value === 'string2') { // {{#eq data.value 'string2'}}
    return 'blank';
  }
  else { // {{else}}
    return data.value;
  } // {{/eq}}
} // {{/eq}}

Sometimes the templates can get a bit complicated.

2 Likes

Makes sense. Just curious, what’s the difference between #if and #eq?

#if will only check an already existing boolean, it can’t be used to compare things. We added the #eq helper (and others like #gt, #lt, etc) which can be used to do inline comparisons.

1 Like