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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)