簡體   English   中英

如何在golang html/template 中的多個地方創建全局變量和更改?

[英]How to create a global variable and change in multiple places in 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 有人可以幫我解決這個問題嗎?

Go 1.11 添加了對更改模板變量值的支持。 要定義變量,請使用:=

{{$currentUserId := 0}}

要更改其值,請使用 assignment =

{{$currentUserId = .UserData.UserId}}

如果變量是在{{if}}塊之外創建但在其中更改,則更改將在{{if}}塊之后可見。

{{$currentUserId := 0 -}}
Before: {{$currentUserId}}
{{if .UserData -}}
    {{$currentUserId = .UserData.UserId}}
    [<a href="#ask_question">Inside {{$currentUserId}}</a>]
{{else}}
    {{$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 {
    panic(err)

輸出是(在Go Playground上試試):

Before: 0

    [<a href="#ask_question">Inside 99</a>]

[<a href="#ask_question">outside 99</a>]

原答案如下。


簡短的回答是:你不能。

根據設計理念,模板不應包含復雜的邏輯。 在模板中,您只能創建新變量,不能更改現有模板變量的值。 當您在{{if}}塊內執行{{$currentUserId := .UserData.UserId}} ,會創建一個新變量,該變量{{$currentUserId := .UserData.UserId}}外部變量,其范圍擴展到{{end}}操作。

這在text/template包的變量部分中有描述(同樣適用於html/template包):

變量的范圍擴展到聲明它的控制結構(“if”、“with”或“range”)的“結束”操作,如果沒有這樣的控制結構,則擴展到模板的末尾。 模板調用不會從其調用點繼承變量。

可能的解決方法

在您的情況下,最簡單的方法是將自定義函數CurrentUserId()注冊到您的模板,如果UserData存在,則返回其 ID UserData.UserId() ,如果不存在,則返回0與您的情況一樣。

這是一個如何完成的示例:

type UserData struct {
    UserId int
}

func main() {
    m := map[string]interface{}{}
    t := template.Must(template.New("").Funcs(template.FuncMap{
        "CurrentUserId": func() int {
            if u, ok := m["UserData"]; ok {
                return u.(UserData).UserId
            }
            return 0
        },
    }).Parse(src))

    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
    m["UserData"] = UserData{99}
    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
}

const src = `Current user id: {{CurrentUserId}}
`

它首先執行沒有UserData的模板,然后執行具有UserId = 99 UserData的模板。 輸出是:

Current user id: 0
Current user id: 99

Go Playground上試試。

模擬可變變量

您還可以模擬可變變量,但也可以使用已注冊的自定義函數。 你可以注冊一個像SetCurrentUserId()這樣的函數,它會改變一個變量的值,最好是在傳遞給模板執行的參數中,這樣它就可以安全地並發使用。

這是一個如何做到的示例(它使用map作為模板數據,並且SetCurrentUserId()將當前用戶 ID 設置為該地圖中的一個值):

func main() {
    m := map[string]interface{}{}
    t := template.Must(template.New("").Funcs(template.FuncMap{
        "SetCurrentUserId": func(id int) string {
            m["CurrentUserId"] = id
            return ""
        },
    }).Parse(src))

    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
    m["UserData"] = UserData{99}
    if err := t.Execute(os.Stdout, m); err != nil {
        panic(err)
    }
}

const src = `Before: {{.CurrentUserId}}
{{if .UserData}}
    {{SetCurrentUserId .UserData.UserId}}Inside: {{.CurrentUserId}}
{{else}}
    {{SetCurrentUserId 0}}Inside: {{.CurrentUserId}}
{{end}}
After: {{.CurrentUserId}}
`

這再次首先執行沒有UserData的模板,然后執行具有UserId = 99 UserData模板。 輸出是:

Before: 
    Inside: 0
After: 0
Before: 0
    Inside: 99
After: 99

Go Playground上試試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM