Search This Blog

Saturday, August 24, 2019

Handling map keys spacing in templates

If you ever came across a similar JSON map to this:

{
   "Update"      : "2019-08-24T12:34:56.789Z",
   "Stats"       : {
      "Memory stats": {
         "Free memory" : 1234,
         "Total memory": 5678
      }
   }
}

You know that to access JSON map keys with space in javascript you need to do something like this:

print("Update: " + myMap.Update);
print("Free mem: " + myMap.Stats["Memory stats"]["Free memory"] + "MB");

To replicate in a HandleBars template you would write for the first line:

Update: {{Update}}

But, how to access the keys with space in HandleBars?

Update: {{Update}}
Free mem: {{Stats.[Memory stats].[Free memory]}}MB

So the final code would be, for example:

tprint("Update: {{Update}}", myMap);
tprint("Free mem: {{Stats.[Memory stats].[Free memory]}}MB", myMap);

No comments:

Post a Comment

Using arrays with parallel

OpenAF is a mix of Javascript and Java, but "pure" javascript isn't "thread-safe" in the Java world. Nevertheless be...