go - 如何在golang html/template中的多个地方创建全局变量和更改?

我正在和中创建一个变量html/template并根据条件更改该值。但是值的范围只停留在if条件内:


{{if .UserData}}

    {{$currentUserId := .UserData.UserId}}

    [<a href="#ask_question">Inside {{$currentUserId}}</a>]

{{else}}

    {{$currentUserId := 0}}

{{end}}

[<a href="#ask_question">outside {{$currentUserId}}</a>]

在 if 条件内,我得到了正确的值,但在它之外是0. 如何使用$currentUserId外部条件?有人可以帮我解决这个问题吗?


拉丁的传说
浏览 193回答 1
1回答

蛊毒传说

Go 1.11 添加了对更改模板变量值的支持。要定义变量,请使用:=:{{$currentUserId := 0}}要更改其值,请使用 assignment =:{{$currentUserId = .UserData.UserId}}如果变量是在{{if}}块外部创建但在块内部更改,则更改将在{{if}}块之后可见。{{$currentUserId := 0 -}}Before: {{$currentUserId}}{{if .UserData -}}&nbsp; &nbsp; {{$currentUserId = .UserData.UserId}}&nbsp; &nbsp; [<a href="#ask_question">Inside {{$currentUserId}}</a>]{{else}}&nbsp; &nbsp; {{$currentUserId = 0}}{{end}}[<a href="#ask_question">outside {{$currentUserId}}</a>]像这样测试:m := map[string]interface{}{}t := template.Must(template.New("").Parse(src))m["UserData"] = UserData{99}if err := t.Execute(os.Stdout, m); err != nil {&nbsp; &nbsp; panic(err)输出是(在Go Playground上试试):Before: 0&nbsp; &nbsp; [<a href="#ask_question">Inside 99</a>][<a href="#ask_question">outside 99</a>]原答案如下。简短的回答是:你不能。根据设计理念,模板不应包含复杂的逻辑。在模板中,您只能创建新变量,不能更改现有模板变量的值。当您{{$currentUserId := .UserData.UserId}}在{{if}}块内执行时,会创建一个新变量,该变量会影响外部变量,并且其范围扩展到{{end}}操作。这在text/template包的变量部分中有描述(同样适用于html/template包):变量的范围扩展到声明它的控制结构(“if”、“with”或“range”)的“结束”操作,如果没有这样的控制结构,则扩展到模板的末尾。模板调用不会从其调用点继承变量。可能的解决方法在您的情况下,最简单的方法是将自定义函数注册CurrentUserId()到您的模板,如果UserData存在,则返回其 id UserData.UserId(),如果不存在,则0按照您的情况返回。这是一个如何完成的示例:type UserData struct {&nbsp; &nbsp; UserId int}func main() {&nbsp; &nbsp; m := map[string]interface{}{}&nbsp; &nbsp; t := template.Must(template.New("").Funcs(template.FuncMap{&nbsp; &nbsp; &nbsp; &nbsp; "CurrentUserId": func() int {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if u, ok := m["UserData"]; ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return u.(UserData).UserId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }).Parse(src))&nbsp; &nbsp; if err := t.Execute(os.Stdout, m); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; m["UserData"] = UserData{99}&nbsp; &nbsp; if err := t.Execute(os.Stdout, m); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}const src = `Current user id: {{CurrentUserId}}`它首先执行模板没有UserData,然后将其与执行模板UserData有UserId = 99。输出是:Current user id: 0Current user id: 99在Go Playground上试试。模拟可变变量您还可以模拟可变变量,但也可以使用已注册的自定义函数。您可以注册一个类似的函数SetCurrentUserId()来改变变量的值,最好是在传递给模板执行的参数中,这样它就可以安全地并发使用。这是一个如何做到的示例(它使用 amap作为模板数据,并将SetCurrentUserId()当前用户 ID 设置为该地图中的一个值):func main() {&nbsp; &nbsp; m := map[string]interface{}{}&nbsp; &nbsp; t := template.Must(template.New("").Funcs(template.FuncMap{&nbsp; &nbsp; &nbsp; &nbsp; "SetCurrentUserId": func(id int) string {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m["CurrentUserId"] = id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ""&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }).Parse(src))&nbsp; &nbsp; if err := t.Execute(os.Stdout, m); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; m["UserData"] = UserData{99}&nbsp; &nbsp; if err := t.Execute(os.Stdout, m); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}const src = `Before: {{.CurrentUserId}}{{if .UserData}}&nbsp; &nbsp; {{SetCurrentUserId .UserData.UserId}}Inside: {{.CurrentUserId}}{{else}}&nbsp; &nbsp; {{SetCurrentUserId 0}}Inside: {{.CurrentUserId}}{{end}}After: {{.CurrentUserId}}`这再次首先执行没有 的模板UserData,然后执行UserData具有的模板UserId = 99。输出是:Before:&nbsp;&nbsp; &nbsp; Inside: 0After: 0Before: 0&nbsp; &nbsp; Inside: 99After: 99在Go Playground上试试。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go