英文:
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:"id"`
UserId string `db:"user_id"`
Data string `db:"data"`
CreatedAt time.Time `db:"created_at"`
}
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("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>
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}}
<b>{{name}}</b>
{{/repo}}
哈希:
{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" }
]
}
输出:
<b>resque</b>
<b>hub</b>
<b>rip</b>
我建议尝试以下代码:
viewModel := struct{
items []DataKey{}
}{
data
}
mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, viewModel )))
然后将模板替换为:
{{#items}}
<tr>
<td>{{Id}}</td>
<td>{{UserId}}</td>
<td>{{Data}}</td>
<td>{{CreatedAt}}</td>
</tr>
{{/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}}
<b>{{name}}</b>
{{/repo}}
Hash:
{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" }
]
}
Output:
<b>resque</b>
<b>hub</b>
<b>rip</b>
I'd suggest trying the following
viewModel := struct{
items []DataKey{}
}{
data
}
mustache.RenderFileInLayout("templates/datakeys.html.mustache", "templates/layout.html.mustache", user, viewModel )))
and then replace the template with
{{#items}}
<tr>
<td>{{Id}}</td>
<td>{{UserId}}</td>
<td>{{Data}}</td>
<td>{{CreatedAt}}</td>
</tr>
{{/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
{
"items": [
{...},{...} ... etc
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论