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>