Handlebars supports the use of helpers, which are custom functions that can be called within templates to perform specific transformations or logic based on the template data.

Build-in Helpers

Handlebars provides several built-in helpers which can be used to add logic to your templates. Here are some of the commonly used ones:

  1. if: Conditionally includes a block of template content.
{{#if condition}}
   Content to display if `condition` is true.
{{/if}}
  1. unless: The inverse of if. Includes a block of content if the expression evaluates to false.
{{#unless condition}}
  Content to display if `condition` is false.
{{/unless}}
  1. each: Iterates over an array, rendering the block once for each item.
{{#each items}}
  {{this}}
{{/each}}
  1. with: Provides a new context for the block within its current scope.
{{#with person}}
  <p>{{ name }}</p>
{{/with}}

Additional helpers

In addition to the built-in helpers provided by Handlebars, we have added a couple more custom helpers to cater to specific needs and enhance the templating capabilities.

  1. compare: Compares two values using a specified operator. Available operators are: ==,===,!=,!==,>,>=,<,<=,|| and&&
{{#compare prop "===" propB}}
  Content to display if `prop === propB` is true.
{{/compare}}
  1. json: Converts an object into a JSON string. Useful if you need to, for example, render charts with a JSON representation of your data. Checkout our reports example to see it in action.
<script>
  var chartData = {{{json chartVariable}}}
</script>

Was this page helpful?