亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以從嵌套模板訪問頂級模板變量嗎?

我可以從嵌套模板訪問頂級模板變量嗎?

Go
交互式愛情 2023-08-14 17:34:11
假設我有一個帶有這樣的嵌套子模板的模板。游樂場鏈接package mainimport (    "os"    "text/template")type Person struct {    FirstName  string    SecondName string}type Document struct {    DocName string    People  []Person}const document = `Document name: {{.DocName}}{{range $person:=.People}}{{template "person" $person}}{{end}}{{- define "person"}}Person name is: {{.FirstName}} {{.SecondName}}{{end}}`func main() {    d := Document{        DocName: "first try",        People: []Person{            {"Brian", "Kernighan"},            {"Dennis", "Ritchie"},        },    }    t := template.Must(template.New("document").Parse(document))    err := t.Execute(os.Stdout, d)    if err != nil {        panic(err)    }}package mainimport (    "os"    "text/template")type Person struct {    FirstName  string    SecondName string}type Document struct {    DocName string    People  []Person}const document = `Document name: {{.DocName}}{{range $person:=.People}}{{template "person" $person}}{{end}}{{- define "person"}}Person name is: {{.FirstName}} {{.SecondName}}{{end}}`func main() {    d := Document{        DocName: "first try",        People: []Person{            {"Brian", "Kernighan"},            {"Dennis", "Ritchie"},        },    }    t := template.Must(template.New("document").Parse(document))    err := t.Execute(os.Stdout, d)    if err != nil {        panic(err)    }}type Person struct {    FirstName  string    SecondName string}type Document struct {    DocName string    People  []Person    SwitchNameOrder bool}const document = `Document name: {{.DocName}}{{range $person:=.People}}{{template "person" $person}}{{end}}{{- define "person"}}{{if $.SwitchNameOrder}} // <---- panic herePerson name is: {{.SecondName}} {{.FirstName}}{{else}}Person name is: {{.FirstName}} {{.SecondName}}{{end}}{{end}}`怎么做?是否可以?
查看完整描述

3 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

您可以做的一件事是使用模板函數將傳遞到子模板的變量與父模板中的變量“合并”。


type Person struct {

    FirstName  string

    SecondName string

}


type Document struct {

    DocName string

    People  []Person


    SwitchNameOrder bool

}


func personWithDocument(p Person, d Document) interface{} {

    return struct {

        Person

        Document Document

    }{p, d}

}


t := template.Must(template.New("document").Funcs(template.FuncMap{

    "personWithDocument": personWithDocument,

}).Parse(document))

然后在模板中您將執行以下操作:


const document = `

Document name: {{.DocName}}


{{range $person:=.People}}

{{template "person" (personWithDocument $person $) }}

{{end}}


{{- define "person"}}

{{if .Document.SwitchNameOrder}}

Person name is: {{.SecondName}} {{.FirstName}}

{{else}}

Person name is: {{.FirstName}} {{.SecondName}}

{{end}}

{{end}}

`

https://play.golang.org/p/YorPsMdr9g_H


查看完整回答
反對 回復 2023-08-14
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

對于上述復雜的解決方案,更好的解決方案是停止嘗試使用頂級配置選項,而是將其編寫為模板函數,并將配置變量放在函數閉包中


{{- define "person"}}

{{if SwitchNameOrder}}

Person name is: {{.SecondName}} {{.FirstName}}

{{else}}

Person name is: {{.FirstName}} {{.SecondName}}

{{end}}

{{end}}


t := template.Must(template.New("document").Funcs(template.FuncMap{

    "SwitchNameOrder": func() bool {

        return switchNames // variable sits in closure

    },

}).Parse(document))

https://play.golang.org/p/O6QHtmxweOi


另一種選擇是將整個切換編寫為字符串函數,即:


{{- define "person"}}

Person name is: {{SwitchNames .FirstName .SecondName}}

{{end}}

并SwitchNames作為字符串函數


...Funcs(template.FuncMap{

    "SwitchNames": func(first, second string) string {

        if switchNames {

            return second + " " + first

        }

        return first + " " + second

    },

})...

可以更干凈或更干凈,具體取決于實際的復雜性


https://play.golang.org/p/UPB3NIpzw0N


查看完整回答
反對 回復 2023-08-14
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

我最終做的是添加一個單獨的結構配置并將其復制到各處。那是,


type Config struct {

    SwitchNameOrder bool

}


type Person struct {

    FirstName  string

    SecondName string


    Config Config 

    // this could also be a pointer,

    // but I don't want to deal with nils, so let's copy

}


type Document struct {

    DocName string

    People  []Person


    Config Config

}


c := Config{SwitchNameOrder: true}


d.Config = c

for _, p := range d.People {

    p.Config = c

}

然后在模板中使用它



{{- define "person"}}

{{if .Config.SwitchNameOrder}}

Person name is: {{.SecondName}} {{.FirstName}}

{{else}}

Person name is: {{.FirstName}} {{.SecondName}}

{{end}}

{{end}}

很丑,但是該怎么辦


https://play.golang.org/p/f95i4me8XLP


查看完整回答
反對 回復 2023-08-14
  • 3 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號