英文:
How to range through a slice of structs in the Iris Go framework?
问题
我正在尝试在Go的Iris 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}}
等方法,但都没有成功。我得到的错误信息如下:
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框架中有效地遍历上述结构体切片?谢谢。
英文:
I am trying to range through a slice of structs in iris the golang web framework as follows.
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>
I have tried {{range .}}
, {{for _posts := range Posts}}
.etc which have not worked?
Here is the error I get
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
How would I be able to range through a slice of structs as seen above effectively in the Go Iris framework?
Thanks
答案1
得分: 2
通过将以下示例中的for post :=
替换为{{range .List}}
来解决了问题。
{{range .List}}
<tr>
<td><input class="checkbox" type="checkbox" name="category" value=""></td>
<td>{{.Id}}</td>
<td>{{.Title}}</td>
<td>{{.Slug}}</td>
</tr>
{{end}}
英文:
Fixed the problem by removing for post :=
in the following example with {{range .List}}
{{range .List}}
<tr>
<td><input class="checkbox" type="checkbox" name="category" value=""></td>
<td>{{.Id}}</td>
<td>{{.Title}}</td>
<td>{{.Slug}}</td>
</tr>
{{end}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论