我正在尝试按如下方式遍历 iris 和 golang web 框架中的一片结构。
type prodcont struct{
List []Post
}
type Post struct{
Id int
Title string
Slug string
ShortDescription string
Content string
}
var Posts = []Post{
Post{content ommitted}
}
//GET categories
func IndexPost(c *iris.Context){
c.Render("admin/post/index.html", prodcont{Posts}, iris.RenderOptions{"gzip": true})
}
<table class="table table-striped table-bordered">
<thead>
<thead>
table head...
</thead>
</thead>
<tbody>
{{run range here}}
<tr>
<td>{{post.Id}}</td>
<td>{{post.Title}}</td>
<td>{{post.Slug}}</td>
<td>{{post.Shortdescription}}</td>
<td>{{post.Content}}</td>
</tr>
{{end}}
</tbody>
</table>
我试过 {{range .}}
, {{for _posts := range Posts}}
.etc 都没有用?
这是我得到的错误
template: template/admin_template.html:61:7: executing "template/admin_template.html" at <yield>: error calling yield: html/template: "admin/post/index.html" is an incomplete template
我如何才能在 Go Iris 框架中有效地遍历一片结构,如上所示? 谢谢
最佳答案
通过在以下示例中使用 {{range .List}}
删除 for post :=
来解决问题
{{range .List}}
<tr>
<td><input class="checkbox" type="checkbox" name="category" value=""></td>
<td>{{.Id}}</td>
<td>{{.Title}}</td>
<td>{{.Slug}}</td>
</tr>
{{end}}
关于loops - 如何遍历 Iris Go 框架中的一片结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706336/