loops - 如何遍历 Iris Go 框架中的一片结构?

标签 loops go go-templates go-iris

我正在尝试按如下方式遍历 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/

相关文章:

go - GC是否处理数组的未指向部分

go - 就地更改函数内的 slice 内容和容量

go - 如何使 template.Execute() 写入文件而不是 response.Writer?

go - 在匿名映射中指定字段名称

java - for循环只循环1次JAVA?

c++ - 如何在不使用数组的情况下存储输入?

c++ - 如何将输入发送到连续循环中? (C++)

C 问题:另一个 while 循环内的 while(fgets) 循环仅执行一次

go - Foo 类型的点 function() foo

go - 使用传递的参数渲染部分模板