第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用動態(tài)名稱調(diào)用其他模板

使用動態(tài)名稱調(diào)用其他模板

Go
富國滬深 2021-07-07 14:01:05
我看不到使用動態(tài)名稱調(diào)用模板(文本或 html)的方法。例子:這有效:{{template "Blah" .}}此錯誤與“模板調(diào)用中出現(xiàn)意外的“$BlahVar””:{{$BlahVar := "Blah"}} {{template $BlahVar .}}我試圖解決的總體問題是我需要根據(jù)配置文件有條件地呈現(xiàn)模板 - 所以我不知道模板的名稱提前。顯然我可以在 FuncMap 中放置一個函數(shù),它只執(zhí)行單獨的模板解析和調(diào)用并返回該結(jié)果,但想知道是否有更好的方法。
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經(jīng)驗 獲得超8個贊

作為對此的說明和跟進,我最終得到了這個問題的兩個主要答案:1)盡量避免這種情況。在某些情況下,一個簡單的 if 語句工作正常。2)我能夠使用 FuncMap 中的一個函數(shù)來完成這個,它只是一個單獨的渲染。這不是世界上最偉大的事情,但它確實有效并解決了問題。這是一個完整的獨立演示,展示了這個想法:


package main


import (

    "bytes"

    "html/template"

    "os"

)


func main() {


    var err error


    // our main template here calls a sub template

    tpl := template.New("main")


    // provide a func in the FuncMap which can access tpl to be able to look up templates

    tpl.Funcs(map[string]interface{}{

        "CallTemplate": func(name string, data interface{}) (ret template.HTML, err error) {

            buf := bytes.NewBuffer([]byte{})

            err = tpl.ExecuteTemplate(buf, name, data)

            ret = template.HTML(buf.String())

            return

        },

    })


    // this is the main template

    _, err = tpl.Parse(`


{{$Name := "examplesubtpl"}}


from main template


{{CallTemplate $Name .}}


`)

    if err != nil {

        panic(err)

    }


    // whatever code to dynamically figure out what templates to load


    // a stub just to demonstrate

    _, err = tpl.New("examplesubtpl").Parse(`


this is from examplesubtpl - see, it worked!


`)

    if err != nil {

        panic(err)

    }


    err = tpl.Execute(os.Stdout, map[string]interface{}{})

    if err != nil {

        panic(err)

    }


}


查看完整回答
反對 回復 2021-07-12
  • 3 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號