英文:
Go: display array in array with templates
问题
如何在Go模板中插入变量,就像这样 - 我在HTML中有这段代码:
{{define "homepage"}}
<html>
<form action="/home/delete" method="POST">
{{with .Posts}}
{{range .}}
<p id="twt">{{ range $i := .Status}}{{$i}}<br><br></p>
<button type="submit" id="xbutton" name="xdel" value="{{.Tweetid}}">Delete</button>
{{end}}
{{end}}
</form>
</html>
{{end}}
Go代码如下:
type User struct {
Userid int64
Username string
Password string
Posts []*Post
}
type Post struct {
Tweetid int
Username string
Status []string
}
func deletehandler(w http.ResponseWriter, r *http.Request) {
currentuser = getUserName(r)
postvalue = r.PostFormValue("xdel")
DeleteTweet()
if currentuser != "" {
as := Post{Username: currentuser, Status: ReadStatus(), Tweetid: ReadStatusId()}
person := User{Username: currentuser, Posts: []*Post{&as}}
t := template.Must(template.New("tele").ParseFiles("layout/home.html"))
if err := t.ExecuteTemplate(w, "homepage", person); err != nil {
panic(err)
}
} else {
http.Redirect(w, r, "/", 302)
}
}
//获取.Tweetid
func ReadStatusId() (res int) {
//打开和访问SQL数据库的一些代码
rows, _ := db.Query("Select id from posts where tweet = ?", AddTweet)
//一些错误处理的代码
defer rows.Close()
var status string
for rows.Next() {
err := rows.Scan(&status)
if err != nil {
fmt.Println(err)
}
fmt.Printf("this %s", status)
}
return
}
func main() {
//(其他处理程序的代码...)
router.HandleFunc("/home/delete", deletehandler).Methods("POST")
}
然而,我得到的错误消息是:
can't evaluate field Tweetid in type string
如何修复这个问题并允许在值字符串中读取.Tweetid?如果有帮助的话,我参考了这个链接中的模板用法:http://jan.newmarch.name/go/template/chapter-template.html
英文:
How do I insert a variable in a Go template like this - I have this code in HTML:
{{define "homepage"}}
<html>
<form action="/home/delete" method="POST">
{{with .Posts}}
{{range .}}
<p id="twt">{{ range $i := .Status}}{{$i}}<br><br></p>
<button type="submit" id="xbutton" name="xdel" value="{{.Tweetid}}">Delete</button>
{{end}}
{{end}}
{{end}}
</form>
</html>
{{end}}
The code in Go:
type User struct {
Userid int64
Username string
Password string
Posts []*Post
}
type Post struct {
Tweetid int
Username string
Status []string
}
func deletehandler(w http.ResponseWriter, r *http.Request) {
currentuser = getUserName(r)
postvalue = r.PostFormValue("xdel")
DeleteTweet()
if currentuser != "" {
as := Post{Username: currentuser, Status: ReadStatus(), Tweetid: ReadStatusId()}
person := User{Username: currentuser, Posts: []*Post{&as}}
t := template.Must(template.New("tele").ParseFiles("layout/home.html"))
if err := t.ExecuteTemplate(w, "homepage", person); err != nil {
panic(err)
}
} else {
http.Redirect(w, r, "/", 302)
}
}
//getting the .Tweetid
func ReadStatusId() (res int) {
//some code to open and access the sql database
rows, _ := db.Query("Select id from posts where tweet = ?", AddTweet)
some code for error handling
defer rows.Close()
var status string
for rows.Next() {
err := rows.Scan(&status)
if err != nil {
fmt.Println(err)
}
fmt.Printf("this %s", status)
}
return
}
func main() {
(//code for other handlers..)
router.HandleFunc("/home/delete", deletehandler).Methods("POST")
}
However, the error message I get is
can't evaluate field Tweetid in type string
How do I fix this and allow .Tweetid to be read in the value string? If this helps I referred to this for the way I've used the templates: http://jan.newmarch.name/go/template/chapter-template.html
答案1
得分: 3
尝试更改模板:
{{range $p := .Posts}}
<p id="twt">{{ range $i := .Status}}{{$i}}<br><br></p>
<button type="submit" id="xbutton" name="xdel" value="{{$p.Tweetid}}">删除</button>
{{end}}
{{end}}
在 {{ range $i := ... }}
中使用 {{.Tweetid}}
将引用 {{$i.Tweetid}}
,添加 range $p := .Posts}}
并明确使用 {{$p.Tweetid}}
引用它将解决问题。
英文:
Try to change the template:
{{range $p := .Posts}}
<p id="twt">{{ range $i := .Status}}{{$i}}<br><br></p>
<button type="submit" id="xbutton" name="xdel" value="{{$p.Tweetid}}">Delete</button>
{{end}}
{{end}}
using {{.Tweetid}}
inside the {{ range $i := ... }}
would reference {{$i.Tweetid}}
adding range $p := .Posts}}
and referencing it explicitly with {{$p.Tweetid}}
will solve the problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论