recursion - Accessing variable from parent object in go html template -
i have recursive go html templates showing replies post, top parent object contains field called random , field called replies, recurse on replies, don't have random field, want use same random field in each reply.
when try goes 2 replies deep (i assume because $ references parent object , not "top" parent object.
edit: more code. thread original object passed template, thread.replies looped on recursively, don't contain thread.random string. want make thread.random string available globally children without restructuring structs.
type thread struct { random string body string title string replies []*reply } type reply struct { body string title string replies []*reply } template:
<html> <body> <div id="posts"> <p>.title</p> <p>.body</p> {{define "replies"}} <ul> {{ range $key, $value := .replies }} <p>$value.title</p> <p>$value.body</p> <p>$.random</p> {{template "replies" . }} </li> {{end}} </ul> {{end}} <div id="posts"> <div class="replypost">{{ template "replies" .}}</div> </div> </body> </html>
whatever want access in template must passed it. there no such thing "this struct field of other struct , want magically access other struct".
Comments
Post a Comment