英文:
Why does Handlebars think that my list is empty?
问题
以下是您要翻译的代码部分:
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);
希望这对您有所帮助。如果您有其他问题,请随时提出。
英文:
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
得分: 1
each
循环的开始看起来是这样的:
{{#each entry}}
提供给渲染引擎的模型只是一个集合,它没有 entry
属性。请改用 this
。
{{#each this}}
这意味着您想要遍历整个模型 - 即您提供给 template()
的 plz
变量,它的类型是 List<TemplateParams>
,而不是 List 对象的不存在属性。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论