golang模板内的变量

时间:2015-10-08 22:35:52

标签: templates variables go go-templates

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

3 个答案:

答案 0 :(得分:11)

修改:有关更新的答案,请参阅https://stackoverflow.com/a/52925780/1685538

原始答案:

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

  

变量的范围扩展到控件的“结束”操作   声明它的结构(“if”,“with”或“range”),或者   如果没有这样的控制结构,模板的结尾。

因此,您使用$prev_year定义的{{$prev_year:=$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)
    }
}

答案 1 :(得分:4)

在go1.11 text/template and hence html/template became able to set the value of existing variables中,这意味着可以对原始代码进行很小的修改就可以使用。

更改

{{$prev_year:=$year}}

收件人

{{$prev_year = $year}}

Playground

答案 2 :(得分:2)

this回答所述,该变量“重新分配”的范围以{{end}}块结束。因此,使用标准变量,无法解决问题,应该在执行模板的Go程序中解决。

在某些框架中,这并不容易(例如protoc-gen-gotemplate)。

Sprig library为标准模板语言添加了额外的功能。其中一个是可变映射,可以按以下方式使用:

// init the dictionary (you can init it without initial key/values as well)
{{$myVar := dict "key" "value"}}

// getting the "key" from the dictionary (returns array) and then fetching the first element from that array
{{pluck "key" $myVar | first}}

// conditional update block
{{if eq "some" "some"}}
     // the $_ seems necessary because Go template functions need to return something
     {{$_ := set $myVar "key" "newValue"}}
{{end}}

// print out the updated value
{{pluck "key" $myVar | first}}

这个小例子打印出来:

value
newValue

实用的方法是对所有可变变量使用单个字典,并将它们作为键存储在相应的变量名下。

参考: