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

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

Slice of structs to unsorted list with mustache

问题

我有一个结构体。

type DataKey struct {
    Id        int64     `db:"id"`
    UserId    string    `db:"user_id"`
    Data      string    `db:"data"`
    CreatedAt time.Time `db:"created_at"`
}

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

data := []DataKey{}

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

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

datakeys.html.mustache

<table class="table table-striped">
<thead>
    <tr>
        <th>#</th>
        <th>UserID</th>
        <th>DataKey</th>
        <th>CreatedAt</th>
    </tr>
</thead>
{{#DataKey}}
    <tr>
        <td>{{Id}}</td>
        <td>{{UserId}}</td>
        <td>{{Data}}</td>
        <td>{{CreatedAt}}</td>
    </tr>
{{/DataKey}}
</table>

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

英文:

I have a struct.

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

I create a slice of structs.

data := []DataKey{}

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

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

datakeys.html.mustache

&lt;table class=&quot;table table-striped&quot;&gt;
&lt;thead&gt;
    &lt;tr&gt;
	    &lt;th&gt;#&lt;/th&gt;
	    &lt;th&gt;UserID&lt;/th&gt;
	    &lt;th&gt;DataKey&lt;/th&gt;
	    &lt;th&gt;CreatedAt&lt;/th&gt;
    &lt;/tr&gt;
&lt;/thead&gt;
{{#DataKey}}
    &lt;tr&gt;
		&lt;td&gt;{{Id}}&lt;/td&gt;
		&lt;td&gt;{{UserId}}&lt;/td&gt;
		&lt;td&gt;{{Data}}&lt;/td&gt;
		&lt;td&gt;{{CreatedAt}}&lt;/td&gt;
    &lt;/tr&gt;
{{/DataKey}}
&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}} 是错误的。

根据文档:

模板:

{{#repo}}
  &lt;b&gt;{{name}}&lt;/b&gt;
{{/repo}}

哈希:

{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" }
  ]
}

输出:

&lt;b&gt;resque&lt;/b&gt;
&lt;b&gt;hub&lt;/b&gt;
&lt;b&gt;rip&lt;/b&gt;

我建议尝试以下代码:

viewModel := struct{
    items []DataKey{}
}{
    data
}

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

然后将模板替换为:

{{#items}}
    &lt;tr&gt;
        &lt;td&gt;{{Id}}&lt;/td&gt;
        &lt;td&gt;{{UserId}}&lt;/td&gt;
        &lt;td&gt;{{Data}}&lt;/td&gt;
        &lt;td&gt;{{CreatedAt}}&lt;/td&gt;
    &lt;/tr&gt;
{{/items}}

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

为了更清楚,理论上

viewModel := struct{
    items []DataKey{}
}{
    data
}

将变成

{
    "items": [
        {...},{...} ... 等等
    ]
}
英文:

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

From the docs:

Template:

{{#repo}}
  &lt;b&gt;{{name}}&lt;/b&gt;
{{/repo}}

Hash:

{
  &quot;repo&quot;: [
    { &quot;name&quot;: &quot;resque&quot; },
    { &quot;name&quot;: &quot;hub&quot; },
    { &quot;name&quot;: &quot;rip&quot; }
  ]
}

Output:

&lt;b&gt;resque&lt;/b&gt;
&lt;b&gt;hub&lt;/b&gt;
&lt;b&gt;rip&lt;/b&gt;

I'd suggest trying the following

viewModel := struct{
    items []DataKey{}
}{
    data
}

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

and then replace the template with

{{#items}}
    &lt;tr&gt;
        &lt;td&gt;{{Id}}&lt;/td&gt;
        &lt;td&gt;{{UserId}}&lt;/td&gt;
        &lt;td&gt;{{Data}}&lt;/td&gt;
        &lt;td&gt;{{CreatedAt}}&lt;/td&gt;
    &lt;/tr&gt;
{{/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

viewModel := struct{
    items []DataKey{}
}{
    data
}

will become

{
    &quot;items&quot;: [
        {...},{...} ... etc
    ]
}

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:

确定