Handlebars为什么认为我的列表是空的?

huangapple go评论63阅读模式
英文:

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 =
                @&quot;&lt;table&gt;
            	&lt;tr&gt;
            		&lt;th&gt;File Name&lt;/th&gt;
            		&lt;th&gt;Error&lt;/th&gt;
            		&lt;th&gt;Size&lt;/th&gt;
                    &lt;th&gt;Date&lt;/th&gt;
                    &lt;th&gt;Testing&lt;/th&gt;
            	&lt;/tr&gt;
            	{{#each entry}}
            		&lt;tr&gt;
            			&lt;td&gt;{{fname}}&lt;/td&gt;
            			&lt;td&gt;{{error}}&lt;/td&gt;
            			&lt;td&gt;{{size}}&lt;/td&gt;
                        &lt;td&gt;{{data}}&lt;/td&gt;
                        &lt;td&gt;test data&lt;/td&gt;
            		&lt;/tr&gt;
            	{{/each}}
            &lt;/table&gt;&quot;;

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

var plz = JsonConvert.DeserializeObject&lt;List&lt;TemplateParams&gt;&gt;(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&lt;TemplateParams&gt;,而不是 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&lt;TemplateParams&gt; - rather than a non-existent property of the List object.

huangapple
  • 本文由 发表于 2023年3月4日 03:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定