2

There will be 2 json, one will contain the constant json other will have the original values. Consider this for eg,

{
  constant : {address: "location"},
  data : {location : "india"}
}

constant and data are the two json object.

<div class="entry">
  <div class="body">
     {{data.[constant.address]}}
  </div>
</div>

It is not retrieving the expected value 'india'. {{constant.address}} is returing 'location' and {{data.location}} is returning 'india'. I am not sure about this behaviour, any explanation about this behaviour is really helpful. Anyhow i can achieve this with the help of below helper function.

Handlebars.registerHelper('getKey', function(object, key){
    return object[key];
});

In handlebars,

<div class="entry">
  <div class="body">
     {{getKey data constant.address}}
  </div>
</div>

Here, 'india' is getting printed. Thanks in advance.

Raj Kumari
  • 57
  • 1
  • 5
  • Does this answer your question? [Handlebars.js - Access object value with a variable key](https://stackoverflow.com/questions/19646244/handlebars-js-access-object-value-with-a-variable-key) – 76484 Nov 25 '20 at 02:30

1 Answers1

0

You don't need to use the '.' operator after data when you are passing a variable key, simply use [] brackets. Like this :

    <div class="entry">
      <div class="body">
        {{data[constant.address]}}
      </div>
    </div>
Naveen Chahar
  • 561
  • 3
  • 10
  • 1
    Error(s): Error: Parse error on line 4: ....[location]}} {{data[constant.addres ----------------------^ Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID' Got this error on trying without doubts {{data[constant.address]}} – Raj Kumari Nov 24 '20 at 02:23
  • Although using [ ] brackets like this - ' data[constant.address] ' works perfectly in javascript (you can try it in console), but it doesn't work inside handlebars template. – Naveen Chahar Nov 24 '20 at 04:34
  • Maybe this question can help you : https://stackoverflow.com/questions/56636426/expecting-id-string-number-boolean-undefined-null-data-got – Naveen Chahar Nov 24 '20 at 04:35