doulu7921 2017-02-21 18:25
浏览 77
已采纳

在Go语言的HTML模板中范围任意数量的嵌套结构片段

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.

  • 写回答

2条回答 默认 最新

  • dongluoheng3324 2017-02-21 20:35
    关注

    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}}
    

    Playground Example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器