gpt4 book ai didi

templates - golang模板中的变量

转载 作者:IT老高 更新时间:2023-10-28 13:07:03 24 4
gpt4 key购买 nike

html/text 模板中变量的命名空间是什么?我认为变量 $x 可以改变模板内的值,但这个例子告诉我我不能。

当我尝试按年份对锦标赛进行分组时失败了 - 像这样 (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)
}
}

最佳答案

编辑:见 https://stackoverflow.com/a/52925780/1685538以获得最新的答案。


原答案:

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.

所以你用 {{$prev_year:=$year}} 定义的 $prev_year 只存在到.. 下一行 ({{end} })。

似乎没有办法解决这个问题。

执行此操作的“正确”方法是从模板中取出该逻辑,并在 Go 代码中进行分组。

这是一个工作示例: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)
}
}

关于templates - golang模板中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com