Skip to content

Instantly share code, notes, and snippets.

@amitkumar
Last active March 24, 2022 16:32
Show Gist options
  • Select an option

  • Save amitkumar/b1969e86e1379db83511 to your computer and use it in GitHub Desktop.

Select an option

Save amitkumar/b1969e86e1379db83511 to your computer and use it in GitHub Desktop.
Useful Handlebars Helpers. Get specific array item, if equals filter
<script id="if_equals_example" type="text/x-handlebars-template">
{{#if_equals objectType 'message'}}
<div class="message">
Message
</div>
{{else}}
{{/if_equals}}
</script>
<script id="array_item_example" type="text/x-handlebars-template">
<span class="first-friend-name">{{#array_item friends 0}}{{firstName}} {{lastName}}{{/array_item}}</span>
</script>
Handlebars.registerHelper('if_equals', function(conditional, value, options) {
if (conditional === value){
return options.fn(this);
} else {
return options.inverse(this);
}
});
Handlebars.registerHelper('array_item', function(array, index, options) {
return options.fn(array[index]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment