将结构体的切片转换为未排序的列表,使用mustache。

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

Slice of structs to unsorted list with mustache

问题

我有一个结构体。

  1. type DataKey struct {
  2. Id int64 `db:"id"`
  3. UserId string `db:"user_id"`
  4. Data string `db:"data"`
  5. CreatedAt time.Time `db:"created_at"`
  6. }

我创建了一个结构体切片。

  1. data := []DataKey{}

在执行 SQL 查询并填充切片后,我尝试将其传递给 mustache 来构建我的列表。

  1. mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, data))

datakeys.html.mustache

  1. <table class="table table-striped">
  2. <thead>
  3. <tr>
  4. <th>#</th>
  5. <th>UserID</th>
  6. <th>DataKey</th>
  7. <th>CreatedAt</th>
  8. </tr>
  9. </thead>
  10. {{#DataKey}}
  11. <tr>
  12. <td>{{Id}}</td>
  13. <td>{{UserId}}</td>
  14. <td>{{Data}}</td>
  15. <td>{{CreatedAt}}</td>
  16. </tr>
  17. {{/DataKey}}
  18. </table>

我只得到了表头。这个函数没有返回错误,所以我不知道为什么它不喜欢这些数据。我还尝试将其作为引用传递,但结果仍然相同。

英文:

I have a struct.

  1. type DataKey struct {
  2. Id int64 `db:&quot;id&quot;`
  3. UserId string `db:&quot;user_id&quot;`
  4. Data string `db:&quot;data&quot;`
  5. CreatedAt time.Time `db:&quot;created_at&quot;`
  6. }

I create a slice of structs.

  1. data := []DataKey{}

After doing a sql query and filling the slices I try to pass to mustache to build my list.

  1. mustache.RenderFileInLayout(&quot;templates/datakeys.html.mustache&quot;, &quot;templates/layout.html.mustache&quot;, user, data)))

datakeys.html.mustache

  1. &lt;table class=&quot;table table-striped&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;#&lt;/th&gt;
  5. &lt;th&gt;UserID&lt;/th&gt;
  6. &lt;th&gt;DataKey&lt;/th&gt;
  7. &lt;th&gt;CreatedAt&lt;/th&gt;
  8. &lt;/tr&gt;
  9. &lt;/thead&gt;
  10. {{#DataKey}}
  11. &lt;tr&gt;
  12. &lt;td&gt;{{Id}}&lt;/td&gt;
  13. &lt;td&gt;{{UserId}}&lt;/td&gt;
  14. &lt;td&gt;{{Data}}&lt;/td&gt;
  15. &lt;td&gt;{{CreatedAt}}&lt;/td&gt;
  16. &lt;/tr&gt;
  17. {{/DataKey}}
  18. &lt;/table&gt;

The only thing I get is the table header. This function does not return an error so I don't know why it didn't like the data. I have also tried passing it in as a reference.

答案1

得分: 1

我对mustache不太熟悉,但从我看到的内容来看,{{#DataKey}} 是错误的。

根据文档:

模板:

  1. {{#repo}}
  2. &lt;b&gt;{{name}}&lt;/b&gt;
  3. {{/repo}}

哈希:

  1. {
  2. "repo": [
  3. { "name": "resque" },
  4. { "name": "hub" },
  5. { "name": "rip" }
  6. ]
  7. }

输出:

  1. &lt;b&gt;resque&lt;/b&gt;
  2. &lt;b&gt;hub&lt;/b&gt;
  3. &lt;b&gt;rip&lt;/b&gt;

我建议尝试以下代码:

  1. viewModel := struct{
  2. items []DataKey{}
  3. }{
  4. data
  5. }
  6. mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, viewModel )))

然后将模板替换为:

  1. {{#items}}
  2. &lt;tr&gt;
  3. &lt;td&gt;{{Id}}&lt;/td&gt;
  4. &lt;td&gt;{{UserId}}&lt;/td&gt;
  5. &lt;td&gt;{{Data}}&lt;/td&gt;
  6. &lt;td&gt;{{CreatedAt}}&lt;/td&gt;
  7. &lt;/tr&gt;
  8. {{/items}}

这个代码没有经过测试,可能不正确,但值得尝试。我猜测 DataKey 不是模型的属性,因此无法进行评估。

为了更清楚,理论上

  1. viewModel := struct{
  2. items []DataKey{}
  3. }{
  4. data
  5. }

将变成

  1. {
  2. "items": [
  3. {...},{...} ... 等等
  4. ]
  5. }
英文:

Im not familiar with mustache but from looking at it I think the {{#DataKey}} is wrong.

From the docs:

Template:

  1. {{#repo}}
  2. &lt;b&gt;{{name}}&lt;/b&gt;
  3. {{/repo}}

Hash:

  1. {
  2. &quot;repo&quot;: [
  3. { &quot;name&quot;: &quot;resque&quot; },
  4. { &quot;name&quot;: &quot;hub&quot; },
  5. { &quot;name&quot;: &quot;rip&quot; }
  6. ]
  7. }

Output:

  1. &lt;b&gt;resque&lt;/b&gt;
  2. &lt;b&gt;hub&lt;/b&gt;
  3. &lt;b&gt;rip&lt;/b&gt;

I'd suggest trying the following

  1. viewModel := struct{
  2. items []DataKey{}
  3. }{
  4. data
  5. }
  6. mustache.RenderFileInLayout(&quot;templates/datakeys.html.mustache&quot;, &quot;templates/layout.html.mustache&quot;, user, viewModel )))

and then replace the template with

  1. {{#items}}
  2. &lt;tr&gt;
  3. &lt;td&gt;{{Id}}&lt;/td&gt;
  4. &lt;td&gt;{{UserId}}&lt;/td&gt;
  5. &lt;td&gt;{{Data}}&lt;/td&gt;
  6. &lt;td&gt;{{CreatedAt}}&lt;/td&gt;
  7. &lt;/tr&gt;
  8. {{/items}}

This is untested and might not be correct, but might be worth trying. My guess is that DataKey isn't a property on the model hence it's failing to evaluate.

Edit for more clarity: In theory

  1. viewModel := struct{
  2. items []DataKey{}
  3. }{
  4. data
  5. }

will become

  1. {
  2. &quot;items&quot;: [
  3. {...},{...} ... etc
  4. ]
  5. }

huangapple
  • 本文由 发表于 2015年3月26日 23:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/29282728.html
匿名

发表评论

匿名网友

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

确定