访问我的模板中的结构体数组。

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

access my array of structs in my template

问题

我正在传递一个结构体数组到我的模板中,数据已经存在,但我找不到一种访问特定数据的方法,我已经尝试了很多方法,下面是代码:

我的结构体:

type Data struct {
  Destination string
  IData interface{}
}

然后在我的控制器中:

users := []models.User {}

userRow := models.User{Name: "jon", Email: "jon@mail.com"}
users = append(users, userRow)
users2 := users

data := models.Data{
    Destination: "content",
    IData: users,
}

data2 := models.Data{
    Destination: "content",
    IData: users2,
}

dataFinal := []models.Data{}
dataFinal = append(dataFinal, data)
dataFinal = append(dataFinal, data2)

这是我的模板,尽管它似乎没有起作用,它显示了原始数据,但似乎无法访问特定的名称。

{{define "content"}}
<h2>这是正文内容</h2>

<ul>
    {{.}}
    {{range .}}
    <li>{{.}}</li>
    {{end}}
</ul>
{{end}}

编辑:
项目链接:https://github.com/og2/go-og2-mvc

你可能需要运行以下命令:

go get github.com/go-sql-driver/mysql

go get github.com/julienschmidt/httprouter

这样就可以正常工作了!

英文:

I'm passing an array of structs to my template, the data is there but I can't find a way to access specific data, I tried many things already, here it is

My Struct

type Data struct {
  Destination string
  IData interface{}
}

then in my controller I have

users := []models.User {}

userRow := models.User{Name: &quot;jon&quot;, Email: &quot;jon@mail.com&quot;}
users = append(users, userRow)
users2 := users

data := models.Data{
    Destination: &quot;content&quot;,
    IData: users,
}

data2 := models.Data{
    Destination: &quot;content&quot;,
    IData: users2,
}

dataFinal := []models.Data{}
dataFinal = append(dataFinal, data)
dataFinal = append(dataFinal, data2)

and this is my template, though this didn't seem to work, it does show the raw data but can't seem to access the name specifically.

{{define &quot;content&quot;}}
&lt;h2&gt;THIS IS THE BODY CONTENT&lt;/h2&gt;

&lt;ul&gt;
    {{.}}
    {{range .}}
    &lt;li&gt;{{.}}&lt;/li&gt;
    {{end}}
&lt;/ul&gt;
{{end}}

edit:
project: https://github.com/og2/go-og2-mvc

you may wanna run:

go get github.com/go-sql-driver/mysql

go get github.com/julienschmidt/httprouter

for it to work and should be just fine!

答案1

得分: 2

如果你传递给"content"模板执行的管道值是dataFinal,那么你需要使用两个{{range}}操作,因为dataFinal本身是一个切片(类型为[]models.Data),而Data.IData也是一个切片(类型为[]model.User)。

在内部的{{range}}中,你可以使用.Name来引用User.Name

<li>{{.Name}}</li>

看下面的工作示例:

const templ = `{{define "content"}}
<h2>THIS IS THE BODY CONTENT</h2>

<ul>
    {{.}}
    {{range .}}
    <ul>
       {{range .IData}}
       <li>{{.Name}}</li>
       {{end}}
    </ul>
    {{end}}
</ul>
{{end}}`

// 解析和执行模板:
t := template.Must(template.New("").Parse(templ))
fmt.Println(t.ExecuteTemplate(os.Stdout, "content", dataFinal))

输出结果(在Go Playground上尝试):

<h2>THIS IS THE BODY CONTENT</h2>

<ul>
    [{content [{jon jon@mail.com}]} {content [{jon jon@mail.com}]}]

    <ul>
   
       <li>jon</li>
   
    </ul>

    <ul>
   
       <li>jon</li>
   
    </ul>

</ul>
<nil>
英文:

If the pipeline value that you pass to the &quot;content&quot; template execution is dataFinal, then you have to use two {{range}} actions as dataFinal itself is a slice (of type []models.Data), and Data.IData is also a slice (of type []model.User).

Inside the inner {{range}} you may refer to the User.Name like .Name:

&lt;li&gt;{{.Name}}&lt;/li&gt;

See this working example:

const templ = `{{define &quot;content&quot;}}
&lt;h2&gt;THIS IS THE BODY CONTENT&lt;/h2&gt;

&lt;ul&gt;
    {{.}}
    {{range .}}
    &lt;ul&gt;
       {{range .IData}}
       &lt;li&gt;{{.Name}}&lt;/li&gt;
       {{end}}
    &lt;/ul&gt;
    {{end}}
&lt;/ul&gt;
{{end}}`

// Parsing and executing the template:
t := template.Must(template.New(&quot;&quot;).Parse(templ))
fmt.Println(t.ExecuteTemplate(os.Stdout, &quot;content&quot;, dataFinal))

Output (try it on the Go Playground):

&lt;h2&gt;THIS IS THE BODY CONTENT&lt;/h2&gt;

&lt;ul&gt;
    [{content [{jon jon@mail.com}]} {content [{jon jon@mail.com}]}]

    &lt;ul&gt;
   
       &lt;li&gt;jon&lt;/li&gt;
   
    &lt;/ul&gt;

    &lt;ul&gt;
   
       &lt;li&gt;jon&lt;/li&gt;
   
    &lt;/ul&gt;

&lt;/ul&gt;
&lt;nil&gt;

huangapple
  • 本文由 发表于 2016年9月7日 18:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/39367890.html
匿名

发表评论

匿名网友

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

确定