英文:
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: "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)
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 "content"}}
<h2>THIS IS THE BODY CONTENT</h2>
<ul>
{{.}}
{{range .}}
<li>{{.}}</li>
{{end}}
</ul>
{{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 "content"
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
:
<li>{{.Name}}</li>
See this working example:
const templ = `{{define "content"}}
<h2>THIS IS THE BODY CONTENT</h2>
<ul>
{{.}}
{{range .}}
<ul>
{{range .IData}}
<li>{{.Name}}</li>
{{end}}
</ul>
{{end}}
</ul>
{{end}}`
// Parsing and executing the template:
t := template.Must(template.New("").Parse(templ))
fmt.Println(t.ExecuteTemplate(os.Stdout, "content", dataFinal))
Output (try it on the 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论