Colleen Larsen
Colleen Larsen

Reputation: 733

How to range through a slice of structs in the Iris Go framework?

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

Upvotes: 1

Views: 901

Answers (1)

Colleen Larsen
Colleen Larsen

Reputation: 733

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}}

Upvotes: 2

Related Questions