0

I am using Handlebars to handle conditionally building code that ultimately produces a PDF. I am using #if in multiple places, and it works as expected:

<div class="entry">
  {{#if author}}
    <h1>{{author}}</h1>
  {{/if}}
</div>

However, I am wondering about passing a more complex condition rather than simple existence. For instance, what if I want to run this block of code only if the value doesn't equal a particular string:

<div class="entry">
  {{#if author !== 'admin' }}
    <h1>{{author}}</h1>
  {{/if}}
</div>

I also tried this:

<div class="entry">
  {{#if (author !== 'admin') }}
    <h1>{{author}}</h1>
  {{/if}}
</div>

But this throws an error. Is there syntax I can use to do something like this within handlebars?

Ademo
  • 1
  • You would need to create or import a custom helper to do those comparisons. See: https://handlebarsjs.com/guide/expressions.html#helpers . Handlebars does not support equality comparisons directly within the template. – 76484 Apr 26 '21 at 13:50
  • Handlebars does not support this by design. The reasoning is that if complex inline conditions were allowed in templates, that would encourage migration of business logic into the template. – Jon Apr 26 '21 at 14:11

0 Answers0