File Name Error Size Date Testing {{" /> File Name Error Size Date Testing {{" /> File Name Error Size Date Testing {{"/>

Why does Handlebars think that my list is empty?

54 Views Asked by At

Program is written in c# .net

  string mySource =
                @"<table>
                <tr>
                    <th>File Name</th>
                    <th>Error</th>
                    <th>Size</th>
                    <th>Date</th>
                    <th>Testing</th>
                </tr>
                {{#each entry}}
                    <tr>
                        <td>{{fname}}</td>
                        <td>{{error}}</td>
                        <td>{{size}}</td>
                        <td>{{data}}</td>
                        <td>test data</td>
                    </tr>
                {{/each}}
            </table>";

  var json = @"[
  {
    ""fname"": ""testFile1"",
    ""error"": ""true"",
    ""size"": 10,
    ""date"": ""02-15-2023""
  },
  {
    ""fname"": ""testFile2"",
    ""error"": ""false"",
    ""size"": 8,
    ""date"": ""01-15-2023""
  },
  {
    ""fname"": ""testFile3"",
    ""error"": ""true"",
    ""size"": 195,
    ""date"": ""08-23-2021""
  }
]";

var plz = JsonConvert.DeserializeObject<List<TemplateParams>>(json);


var template = Handlebars.Compile(mySource);
var result = template(plz);

As far as I can tell this should work but the program seems to think there is nothing in the list. The only thing that prints are the headers that are hard coded in, and other than that nothing prints.

If anyone has any ideas please let me know

I've tried changing the templates and the objects I am passing into the templates, but I keep getting only the headers to print.

1

There are 1 best solutions below

1
gunr2171 On BEST ANSWER

The start of your each loop looks like this:

{{#each entry}}

The model that you provide to the rendering engine is just a collection, it doesn't have a property entry. Use this instead.

{{#each this}}

This means that you want to loop over the whole model - the plz variable you provide to template(), which is of type List<TemplateParams> - rather than a non-existent property of the List object.