<!DOCTYPE html> <html> <head> <title> Test </title> </head> <body> <div> <h2>Reply</h2> <form action="/post/{{$threadID}}" method="POST"> <input type="text" name="subject" /> <input type="text" name="name" value="Anonymous" /> <input type="text" name="message" /> <input type="submit" value="submit" /> </form> </div> <div> {{range .}} {{$threadID := .ThreadID}} <h3>{{.Subject}}</h3> <h3>{{.Name}}</h3> <div>{{.DatePosted}}</div> <div><p>{{.Text}}</p></div> <br /><br /> {{end}} </div> </body>
I have this template, at the top of the page there is a form that requires threadID from ANY one of the sent messages (they are all the same, a slice of all messages with a specific stream identifier), this obviously does not work, my only other idea was something like
{{range .}} {{if $threadID == nil}} $threadID := .ThreadID //build the form same as above {{end}} <h3>{{.Subject}}</h3> <h3>{{.Name}}</h3> <div>{{.DatePosted}}</div> <div><p>{{.Text}}</p></div> <br /><br /> {{end}}
Here is the Post structure and methods if any of the above issues are unclear.
type Post struct { threadID int subject string name string text string 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 }
And the beginning of the fragment of messages sent to the template
threadID := r.URL.Path[len("/reply/"):] replies, err := i.db.Query("SELECT * FROM latest_threads where thread_id="+threadID);
source share