Referencing a property of a different object in Handlebars



  • I hope this doesn't come across as too stupid. In a data structure from our REST API we get back a big collection if JSON. In one part of this we have information like this:

    'foo': [
      { 'id': integer,
        'time': integer },
      { 'id': otherInteger,
        'time': otherInteger}
      ],
      'bar' : string,
      'bat': otherString
    

    You get the idea. Then in some other part of the JSON we have information that map to the 'id' values in the Array above.

      mappings: {
        'someId' : {
          'name' : 'someName',
          'description': 'string about name'
        },
        'nextId': { ... }
      }
    

    In my template, what I want to do is iterate over the foo Array, but when displaying information about each item in that array, extract the information from the mappings object. I was attempting something like:

    {{#each foo}}
    <h2>Location: {{lookup ../mappings id name}}</h2>
    {{/each}}
    

    But that's not quite right. How can I use the value of the id property from each of the foo objects to lookup another object in mappings and then do things with the properties of THAT object?



  • I think I just answered my own question. You can 'nest' helper calls like lookup, so I am now doing this:

    {{#each foo}}
    <h2>Location: {{lookup (lookup ../mappings id) 'name' }}</h2>
    {{/each}}
    

    Seems to work. Hope this helps someone else!


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.