Search code examples
node.jshandlebars.js

Access object.param in parent context handlebars.js


Not able to get parent object's param value

{{#each myObject.details}}
  <span class="marginL15 pull-left hidden-xs">
    <a href="
      /rates
      /{{../port_data.name}}
      /{{../port_data.name}}
      /{{name}}
      /{{port_code}}
    ">
      {{name}}
    </a>
  </span>
{{/each}}

My object is of format

{
    "meta_title": "fw",
    "port_data": {
        "display_name": "Rajkot (INRAJ), Rajkot, India",
        "name": "Rajkot",
        "port_code": "INRAJ"
    },
    "details": [
        {
            "_id": {
                "$oid": "58f04ef3c0a35f10b7cc08fa"
            },
            "display_name": "Sokhna(Al Sokhna) (EGSOK), Suez, Egypt",
            "name": "Sokhna(Al Sokhna)",
            "port_code": "EGSOK"
        },
        {
            "_id": {
                "$oid": "58ff42cfc0a35f493be68031"
            },
            "display_name": "Rouyn Noranda Airport - CAYUY (YUY), Canada, usa",
            "name": "Rouyn Noranda Airport - CAYUY",
            "port_code": "YUY"
        },
        {
            "_id": {
                "$oid": "58f1c6e9c0a35f2d16dd4f44"
            },
            "display_name": "Sharjah (AESHJ), Sharjah, United Arab Emirates",
            "name": "Sharjah",
            "port_code": "AESHJ"
        }
    ]
}

Also tried {{@root.port_data.name}} but to no use.


Solution

  • When passing myObject.details to the {{#each}} helper, you pass only the referenced array stored in the details field of myObject variable - there is no way to look up to something stored in ../, as there's nothing there.

    What you could do:

    {{#myObject}}
      {{#each details}}
      <span class="marginL15 pull-left hidden-xs">
        <a href="/rates
          /{{../port_data.name}}
          /{{name}}
          /{{port_code}}
        ">
          {{name}}
        </a>
      </span>
      {{/each}}
    {{/myObject}}
    

    It gives you an access to the whole myObject variable one scope above the {{#each}} loop.