Skip to content Skip to sidebar Skip to footer

How To Exclude An Array/object Element With Handlebars?

Right now I have this json structure 'administration' : [ { 'name' : 'Sigfried Bermudez', 'role' : 'Associate Director' }, { 'name' : 'Karen Madrigal', 'role

Solution 1:

Working example: JsFiddle

Use Handlebars helper:

var entry_template = document.getElementById('entry-template').innerHTML;
var data = [
  {
    "name" : "Sigfried Bermudez",
    "role" : "Associate Director"
  },
  {
    "name" : "Karen Madrigal",
    "role" : "QA Manager"
  },
  {
    "name" : "Jorge Lopez",
    "role" : "Project Manager"
  }
]

Handlebars.registerHelper("ifvalue", function(conditional, options) {
  if (conditional !== options.hash.notequals) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

var template = Handlebars.compile(entry_template);
var template_with_data = template(data);

document.getElementById('data').insertAdjacentHTML('afterbegin', template_with_data);

Template:

<script id="entry-template" type="text/x-handlebars-template">
  {{#each this}} 
    {{#ifvalue this.role notequals="Project Manager"}} 
        {{this.name}} 
    {{/ifvalue}} 
  {{/each}}
</script>
<div id="data">
</div>

Post a Comment for "How To Exclude An Array/object Element With Handlebars?"