dp926460 2015-10-08 22:35
浏览 298
已采纳

Golang模板中的变量

What is the namespace of variables inside html/text templates? I thought that a variable $x can change value inside a template, but this example shows me that I cannot.

I failed when I tried to group tournaments according year - something like this (http://play.golang.org/p/EX1Aut_ULD):

package main

import (
    "fmt"
    "os"
    "text/template"
    "time"
)

func main() {
    tournaments := []struct {
        Place string
        Date  time.Time
    }{
        // for clarity - date is sorted, we don't need sort it again
        {"Town1", time.Date(2015, time.November, 10, 23, 0, 0, 0, time.Local)},
        {"Town2", time.Date(2015, time.October, 10, 23, 0, 0, 0, time.Local)},
        {"Town3", time.Date(2014, time.November, 10, 23, 0, 0, 0, time.Local)},
    }
    t, err := template.New("").Parse(`
{{$prev_year:=0}}
{{range .}}
    {{with .Date}}
        {{$year:=.Year}}
                    {{if ne $year $prev_year}}
                        Actions in year {{$year}}:
                {{$prev_year:=$year}}
            {{end}}
    {{end}}

        {{.Place}}, {{.Date}}
    {{end}}

    `)
    if err != nil {
        panic(err)
    }
    err = t.Execute(os.Stdout, tournaments)
    if err != nil {
        fmt.Println("executing template:", err)
    }
}
  • 写回答

3条回答 默认 最新

  • doushanmo7024 2015-10-09 11:25
    关注

    Edit: See https://stackoverflow.com/a/52925780/1685538 for a more up-to-date answer.


    Original answer:

    https://golang.org/pkg/text/template/#hdr-Variables:

    A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared, or to the end of the template if there is no such control structure.

    So the $prev_year you define with {{$prev_year:=$year}} only lives until.. the next line ({{end}}).

    It seems there is no way of going around that.

    The "right" way to do this is to take that logic out of your template, and do the grouping in your Go code.

    Here is a working example : https://play.golang.org/p/DZoSXo9WQR

    package main
    
    import (
        "fmt"
        "os"
        "text/template"
        "time"
    )
    
    type Tournament struct {
        Place string
        Date  time.Time
    }
    
    type TournamentGroup struct {
        Year        int
        Tournaments []Tournament
    }
    
    func groupTournamentsByYear(tournaments []Tournament) []TournamentGroup {
        if len(tournaments) == 0 {
            return nil
        }
    
        result := []TournamentGroup{
            {
                Year:        tournaments[0].Date.Year(),
                Tournaments: make([]Tournament, 0, 1),
            },
        }
    
        i := 0
        for _, tournament := range tournaments {
            year := tournament.Date.Year()
            if result[i].Year == year {
                // Add to existing group
                result[i].Tournaments = append(result[i].Tournaments, tournament)
            } else {
                // New group
                result = append(result, TournamentGroup{
                    Year: year,
                    Tournaments: []Tournament{
                        tournament,
                    },
                })
                i++
            }
        }
    
        return result
    }
    
    func main() {
        tournaments := []Tournament{
            // for clarity - date is sorted, we don't need sort it again
            {"Town1", time.Date(2015, time.November, 10, 23, 0, 0, 0, time.Local)},
            {"Town2", time.Date(2015, time.October, 10, 23, 0, 0, 0, time.Local)},
            {"Town3", time.Date(2014, time.November, 10, 23, 0, 0, 0, time.Local)},
        }
    
        t, err := template.New("").Parse(`
    {{$prev_year:=0}}
    {{range .}}
        Actions in year {{.Year}}:
        {{range .Tournaments}}
    
                {{.Place}}, {{.Date}}
        {{end}}
        {{end}}
    
        `)
        if err != nil {
            panic(err)
        }
        err = t.Execute(os.Stdout, groupTournamentsByYear(tournaments))
        if err != nil {
            fmt.Println("executing template:", err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)