英文:
Range through an arbitrary number of nested slices of structs in an HTML template in Go
问题
我正在尝试构建一个类似Reddit的网络论坛。有顶级帖子和回复,回复可以有回复,依此类推。
一个板块的结构如下:
var board map[string]*Post
以及一个Post
的结构:
type Post struct {
Title string
Body string
ID string
PostNum int
Replies []*Post
}
我该如何使用模板来遍历嵌套的Replies
切片(要记住每个*Post
都包含一个包含*Posts
的Replies
切片,依此类推)?
我目前的进展:
<div id="posts">
{{ .Title }}
{{ .Body }}
<ul>
{{ range $key, $value := .Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
<ul>
{{ range $key, $value := $value.Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
{{ end }}
</ul>
{{ end }}
</ul>
</div>
这只允许我遍历两层回复(并且如你所见使用了重复的代码),我需要能够遍历任意层级的Replies
。
英文:
I'm trying to build a web forum kind of like Reddit. There are top level posts with replies and the replies can have replies and so on.
A board looks like this:
var board map[string]*Post
and a Post
:
type Post struct {
Title string
Body string
ID string
PostNum int
Replies []*Post
}
How would I use a template to range through the nested Replies
slices (keeping in mind that each *Post
contains a Replies
slice that contains *Posts
that contains Replies
and so on)?
What I have so far:
<div id="posts">
{{ .Title }}
{{ .Body }}
<ul>
{{ range $key, $value := .Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
<ul>
{{ range $key, $value := $value.Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
{{ end }}
</ul>
{{ end }}
</ul>
</div>
This only allows me to range through two levels of replies (and as you can see uses duplicate code), I need to be able to range through an arbitrary number of levels of Replies
.
答案1
得分: 7
要递归遍历一个层次结构,可以使用一个执行自身的命名模板。在下面的示例中,模板"replies"执行"replies"以显示子回复:
{{define "main"}}
<div id="post">
{{.Title}}
{{.Body}}
{{template "replies" .Replies}}
</div>
{{end}}
{{define "replies"}}
{{if .}}
<ul>
{{range . }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{.Title}}</b></div>
</div>
<div class="postBody">{{.Body}}</div>
{{template "replies" .Replies}}
</li>
{{end}}
</ul>
{{end}}
{{end}}
英文:
To recurse down a hierarchy, use a named template that executes itself. In the following example, the template "replies" executes "replies" to display sub-replies:
{{define "main"}}
<div id="post">
{{.Title}}
{{.Body}}
{{template "replies" .Replies}}
</div>
{{end}}
{{define "replies"}}
{{if .}}
<ul>
{{range . }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{.Title}}</b></div>
</div>
<div class="postBody">{{.Body}}</div>
{{template "replies" .Replies}}
</li>
{{end}}
</ul>
{{end}}
{{end}}
答案2
得分: 4
你刚刚定义了一个递归数据类型。你可以通过定义一个递归模板来渲染它:
{{define "replies"}}
<ul>
{{ range $key, $value := . }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
{{template "replies" .Replies}}
{{end}}
</ul>
{{end}}
<div id="posts">
{{ .Title }}
{{ .Body }}
{{ template "replies" .Replies }}
</div>
英文:
You just have defined a recursive data type. You can render it by defining a recursive template:
{{define "replies"}}
<ul>
{{ range $key, $value := . }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
{{template "replies" .Replies}}
{{end}}
</ul>
{{end}}
<div id="posts">
{{ .Title }}
{{ .Body }}
{{ template "replies" .Replies }}
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论