如何在Iris Go框架中遍历结构体切片?

huangapple go评论89阅读模式
英文:

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(&quot;admin/post/index.html&quot;, prodcont{Posts}, iris.RenderOptions{&quot;gzip&quot;: true})
}

 
    &lt;table class=&quot;table table-striped table-bordered&quot;&gt;
        &lt;thead&gt;
            &lt;thead&gt;
                table head...
            &lt;/thead&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
   {{run range here}}
    &lt;tr&gt;
        &lt;td&gt;{{post.Id}}&lt;/td&gt;
        &lt;td&gt;{{post.Title}}&lt;/td&gt;
        &lt;td&gt;{{post.Slug}}&lt;/td&gt;
        &lt;td&gt;{{post.Shortdescription}}&lt;/td&gt;
        &lt;td&gt;{{post.Content}}&lt;/td&gt;
    &lt;/tr&gt;
    {{end}}
        &lt;/tbody&gt;
    &lt;/table&gt;

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 &quot;template/admin_template.html&quot; at &lt;yield&gt;: error calling yield: html/template: &quot;admin/post/index.html&quot; 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}}
&lt;tr&gt;
    &lt;td&gt;&lt;input class=&quot;checkbox&quot; type=&quot;checkbox&quot; name=&quot;category&quot; value=&quot;&quot;&gt;&lt;/td&gt;
    &lt;td&gt;{{.Id}}&lt;/td&gt;
    &lt;td&gt;{{.Title}}&lt;/td&gt;
    &lt;td&gt;{{.Slug}}&lt;/td&gt;

&lt;/tr&gt;
{{end}}
英文:

Fixed the problem by removing for post := in the following example with {{range .List}}

 {{range .List}}
&lt;tr&gt;
    &lt;td&gt;&lt;input class=&quot;checkbox&quot; type=&quot;checkbox&quot; name=&quot;category&quot; value=&quot;&quot;&gt;&lt;/td&gt;
    &lt;td&gt;{{.Id}}&lt;/td&gt;
    &lt;td&gt;{{.Title}}&lt;/td&gt;
    &lt;td&gt;{{.Slug}}&lt;/td&gt;

&lt;/tr&gt;
{{end}}

huangapple
  • 本文由 发表于 2016年9月26日 23:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/39706336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定