duanditang2916 2013-11-15 00:16
浏览 55
已采纳

去模板/ HTML迭代从结构生成表

Given a collection of structs, how can I use the "range" template iterator to print out a table that assigns a row per struct, and a column per field value without explicity naming the fields?

container := []Node

type Node struct {
    Contact_id        int
    Employer_id       int
    First_name        string
    Middle_name       string
    Last_name         string
}

Template Code:

{{range .container}}

<tr>
<td>{{.Prefix}}</td>
<td>{{.First_name}}</td>
<td>{{.Middle_name}}</td>
<td>{{.Last_name}}</td>

<td>{{.Contact_id}}</td>
<td>{{.Employer_id}}</td>

</tr>
{{end}}

When I try iterating through the values using

{{range .container}}
 {{range .}}
 <td>{{.}}</td> 
{{end}}
{{end}}

I am told that I cannot iterate over the Values. Is there any clean way to do this?

  • 写回答

1条回答 默认 最新

  • douba9654 2013-11-15 07:45
    关注

    With the html/template, you cannot iterate over the fields in a struct. In the documentation for the package, you can read:

    {{range pipeline}} T1 {{end}}
    The value of the pipeline must be an array, slice, map, or channel.

    That is, Pipeline cannot be a struct. Either you need to:

    • use an intermediate type, eg. [][]interface{}, as container variable that you pass to the template
    • type out each cell separately as you've shown
    • create a template function that converts struct values to some type you can iterate over

    Since a struct is defined at compile-time and won't change its structure during runtime, iteration is not necessary and wouldn't make things more clear in the template. I would advise against it.

    Edit

    But sometimes reflection is a good thing. Brenden also pointed out that you can actually let range iterate over the value returned from a function. If using reflection, this would be the easiest approach.

    Full working example using a template function:

    package main
    
    import (
        "html/template"
        "os"
        "reflect"
    )
    
    type Node struct {
        Contact_id  int
        Employer_id int
        First_name  string
        Middle_name string
        Last_name   string
    }
    
    var templateFuncs = template.FuncMap{"rangeStruct": RangeStructer}
    
    // In the template, we use rangeStruct to turn our struct values
    // into a slice we can iterate over
    var htmlTemplate = `{{range .}}<tr>
    {{range rangeStruct .}} <td>{{.}}</td>
    {{end}}</tr>
    {{end}}`
    
    func main() {
        container := []Node{
            {1, 12, "Accipiter", "ANisus", "Nisus"},
            {2, 42, "Hello", "my", "World"},
        }
    
        // We create the template and register out template function
        t := template.New("t").Funcs(templateFuncs)
        t, err := t.Parse(htmlTemplate)
        if err != nil {
            panic(err)
        }
    
        err = t.Execute(os.Stdout, container)
        if err != nil {
            panic(err)
        }
    
    }
    
    // RangeStructer takes the first argument, which must be a struct, and
    // returns the value of each field in a slice. It will return nil
    // if there are no arguments or first argument is not a struct
    func RangeStructer(args ...interface{}) []interface{} {
        if len(args) == 0 {
            return nil
        }
    
        v := reflect.ValueOf(args[0])
        if v.Kind() != reflect.Struct {
            return nil
        }
    
        out := make([]interface{}, v.NumField())
        for i := 0; i < v.NumField(); i++ {
            out[i] = v.Field(i).Interface()
        }
    
        return out
    }
    

    Output:

    <tr>
        <td>1</td>
        <td>12</td>
        <td>Accipiter</td>
        <td>ANisus</td>
        <td>Nisus</td>
    </tr>
    <tr>
        <td>2</td>
        <td>42</td>
        <td>Hello</td>
        <td>my</td>
        <td>World</td>
    </tr>
    

    Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行