List all key values of a document

Say I have a person document with name, email , address , etc. etc.

How can I display all the key values in a template named person which has the person document as context?

e.g. {{#each key in this}} key : value {{/each}}

name : xyz
email : x@y.z
add: xyz

I am not aware of any such spacebars helper.

But if we take underscore package as standard, you can always get keys by calling _.keys(myJSONObject) .
Creating your own helper with needed functionality should not be problem than.

You can also use Object.keys(some_object), which returns the keys in an array of strings.

You can only perform an {{each}} on a cursor or array, [{},{},…]. Not a json object of json objects, {{},{}…}.

But an array of json objects is fine! Not sure why, but I also never looked too deep.

Once looping thru an an array, the json value can be called by {{key}}.

Best practice just convert your json object of json objects to an array of json objects, then do {{each}} on it.

If you want to print the key and the value, write a helper to get the key from the json and return the exact string, or a new data context for you template that would print it.

1 Like