Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the names of name-value pairs in Handlebars?

I have this data structure:

flavors": {
            "sour": 0.16666666666666666,
            "salty": 0.16666666666666666,
            "sweet": 0,
            "meaty": 0.16666666666666666,
            "bitter": 0.16666666666666666
        }

My html is this:

<p>Flavors:</p>
   <ul>
     {{#Flavors}}
           <li>{{Flavours.Name}}</li>  // doesn't work  //
        {{/Flavors}}
     </ul>

What I'm trying to do is get at the name of the flavour: i.e. salty, sour, etc. I want to be able to cater for arbitrary values is the JSON, and not code them in the html block.

like image 792
mike Avatar asked Dec 26 '22 16:12

mike


1 Answers

You may iterate over the object in this way:

{{#each myObject}}
    Key: {{@key}} Value = {{this}}
{{/each}}

For details check this post: Handlebars/Mustache - Is there a built in way to loop through the properties of an object?

like image 151
Slawomir Pasko Avatar answered Dec 28 '22 05:12

Slawomir Pasko