Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{{#each this}} in handlebars doesn't work if "this" is empty string

consider my json is like this:

{
    main: {
        "" : [{some_obj},{some_obj}]
    },
    secondary: {
        "key": [{some_obj},{some_obj}]
    }
}

Now my first #each will be running for main and secondary.

{{#each this}}
    -- This is for main and secondary --
    {{#each this}}
        -- This is for "" in case of main and "key" in case of secondary --
    {{/each}}
{{/each}} 

My nested #each won't work if "this" is empty as shown in my json for "main" attribute

like image 271
ashy143 Avatar asked Nov 19 '15 00:11

ashy143


1 Answers

Seems like 3.x.x had a bug that caused this. Here's the commit that fixes this in 4.0.0.

I checked these versions with the snippet below:

4.x.x: OK

3.x.x: not OK

2.0.0: OK

var data = {
  main: {
    "": [1, 2, 3]
  },
  secondary: {
    "key": [3, 4, 5]
  }
};
var compiled = Handlebars.compile(document.getElementById('temp').innerHTML);
document.write(compiled(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
<script id="temp" type="text/x-handlebars-template">
  {{#each this}}
    {{@key}}<br>
    {{#each this}}
      &nbsp;&nbsp;"{{@key}}": {{this}}<br>
    {{/each}}
  {{/each}} 
</script>
like image 198
ekuusela Avatar answered Sep 25 '22 14:09

ekuusela