英文:
Accessing struct variable in slice of many structs in html template golang
问题
我正在尝试将包含多个结构体的切片发送到一个HTML模板。
我有一个名为'post'的结构体:
type Post struct {
threadID int
subject string
name string
text string
date_posted string
}
我创建了一个Post
类型的切片(posts := []Post{}
),然后使用来自数据库的行填充该切片,并在模板上执行。
defer latest_threads.Close()
for latest_threads.Next(){
var threadID int
var subject string
var name string
var text string
var date_posted string
latest_threads.Scan(&threadID, &subject, &name, &text, &date_posted)
post := Post{
threadID,
subject,
name,
text,
date_posted,
}
posts = append(posts, post)
}
t, error := template.ParseFiles("thread.html")
if error != nil{
log.Fatal(error)
}
t.Execute(w, posts)
程序编译/运行正常,但在查看模板输出的HTML时,{{.}}
可以正常输出,但是在{{range .}}
中的第一个{{.threadID}}
之后,HTML就停止了。
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<!-- 这里是{{.}},可以正常显示,为了格式化/节省空间已删除 -->
<div>
请注意,我只翻译了代码和HTML部分,其他内容不做翻译。
英文:
I'm attempting to send a slice containing many structs to an html template.
I have a 'post' struct
type Post struct {
threadID int
subject string
name string
text string
date_posted string
}
I create a slice of type Post
( posts := []Post{}
)
this slice is then populated using rows from my database and then executed on my template.
defer latest_threads.Close()
for latest_threads.Next(){
var threadID int
var subject string
var name string
var text string
var date_posted string
latest_threads.Scan(&threadID, &subject, &name, &text, &date_posted)
post := Post{
threadID,
subject,
name,
text,
date_posted,
}
posts = append(posts, post)
}
t, error := template.ParseFiles("thread.html")
if error != nil{
log.Fatal(error)
}
t.Execute(w, posts)
}
The program compiles / runs okay but when viewing the html output from the template
{{.}}
{{range .}}
<div>{{.threadID}}</div>
<h3>{{.subject}}</h3>
<h3>{{.name}}</h3>
<div>{{.date_posted}}</div>
<div><p>{{.text}}</p></div>
<br /><br />
{{end}}
{{.}}
outputs just fine however upon reaching the first {{.threadID}}
in {{range .}}
the html stops.
<!DOCTYPE html>
<html>
<head>
<title> Test </title>
</head>
<body>
//here is where {{.}} appears just fine, removed for formatting/space saving
<div>
答案1
得分: 15
这是要翻译的内容:
这并不是很直观,但是模板(以及像 JSON 这样的编码包)无法访问未导出的数据成员,所以你必须以某种方式将它们导出:
选项 1
// 直接导出字段
type Post struct {
ThreadID int
Subject, Name, Text, DatePosted string
}
选项 2
// 通过访问器公开字段:
type Post struct {
threadID int
subject, name, text, date_posted string
}
func (p *Post) ThreadID() int { return p.threadID }
func (p *Post) Subject() string { return p.subject }
func (p *Post) Name() string { return p.name }
func (p *Post) Text() string { return p.text }
func (p *Post) DatePosted() string { return p.date_posted }
更新模板
(无论你从上面的哪个选项中选择了哪一个,这部分都是必需的)
{{.}}
{{range .}}
<div>{{.ThreadID}}</div>
<h3>{{.Subject}}</h3>
<h3>{{.Name}}</h3>
<div>{{.DatePosted}}</div>
<div><p>{{.Text}}</p></div>
<br /><br />
{{end}}
这样应该可以工作。
英文:
It's not really intuitive, but templates (and encoding packages like JSON, for that matter) can't access unexported data members, so you have to export them somehow:
Option 1
// directly export fields
type Post struct {
ThreadID int
Subject, Name, Text, DatePosted string
}
Option 2
// expose fields via accessors:
type Post struct {
threadID int
subject, name, text, date_posted string
}
func (p *Post) ThreadID() int { return p.threadID }
func (p *Post) Subject() string { return p.subject }
func (p *Post) Name() string { return p.name }
func (p *Post) Text() string { return p.text }
func (p *Post) DatePosted() string { return p.date_posted }
Update template
(this part is mandatory regardless of which option you chose from above)
{{.}}
{{range .}}
<div>{{.ThreadID}}</div>
<h3>{{.Subject}}</h3>
<h3>{{.Name}}</h3>
<div>{{.DatePosted}}</div>
<div><p>{{.Text}}</p></div>
<br /><br />
{{end}}
And this should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论