Check if an item is in array (array contains an element) for Handlebars
<script id="hobbies_template" type="text/x-handlebars-template">
<div>
{{#each hobbies }}
<div style="{{#ifIn id ../favourites }}color: red{{/ifIn}}">
{{ name }}
</div>
{{/each}}
</div>
</script>
<div>
<div style="">
playing football
</div>
<div style="color: red">
reading books
</div>
<div style="color: red">
programming in python
</div>
<div style="">
jogging
</div>
</div>
Handlebars.registerHelper('ifIn', function(elem, list, options) {
if(list.indexOf(elem) > -1) {
return options.fn(this);
}
return options.inverse(this);
});
{
"favourites": [2, 3],
"hobbies": [
{
"name": "playing football",
"id": 1
},
{
"name": "reading books",
"id": 2
},
{
"name": "programming in python",
"id": 3
},
{
"name": "jogging",
"id": 4
}
]
}