I have recursive go html templates for showing replies to a post, the top parent object contains a field called Random
and a field called Replies
, I recurse over the replies, which don't have the Random
field, but I want to use that same Random
field in each reply.
When I try this it only goes two replies deep (I assume because $
only references the parent object and not the "top" parent object.
EDIT: More code. thread
is the original object passed to the template, thread.Replies
are looped over recursively, but don't contain the thread.Random
string. I want to make the thread.Random
string available globally to all the children without restructuring the 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>