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

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

如何從已解析的模板中獲取模板“操作”的地圖或列表?

如何從已解析的模板中獲取模板“操作”的地圖或列表?

Go
HUH函數 2022-04-20 17:54:25
所以我想以某種方式將{{ .blahblah }}模板中定義的所有操作作為字符串切片。例如,如果我有這個模板:<h1>{{ .name }} {{ .age }}</h1>我希望能夠得到[]string{"name", "age"}。假設模板具有方法func (t *Template) Fields() []string:t := template.New("cooltemplate").Parse(`<h1>{{ .name }} {{ .age }}</h1>`)if t.Fields() == []string{"name", "age"} {    fmt.Println("Yay, now I know what fields I can pass in!")    // Now lets pass in the name field that we just discovered.    _ = t.Execute(os.Stdout, map[string]string{"name": "Jack", "age":"120"})}有沒有辦法像這樣檢查已解析的模板?謝謝!
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

前言:正如 Voker 所建議的,該Template.Tree字段“僅導出供 html/template 使用,應被所有其他客戶端視為未導出”。


您不應該依賴這樣的東西來為模板執行提供輸入。您必須知道要執行的模板及其預期的數據。你不應該在運行時“探索”它來為它提供參數。


您從解析模板中獲得的價值是template.Template(text/template或者html/template,它們具有相同的 API)。此模板將模板表示為類型樹parse.Tree。文本模板包含的所有內容都存儲在此樹中的節點中,包括靜態文本、動作等。


話雖如此,您可以遍歷這棵樹并查找標識訪問字段或調用函數的此類操作的節點。節點的類型parse.Node具有Node.Type()返回其類型的方法??赡艿念愋驮诎卸x為常量,parse在類型旁邊parse.NodeType,例如


const (

        NodeText    NodeType = iota // Plain text.

        NodeAction                  // A non-control action such as a field evaluation.

        NodeBool                    // A boolean constant.

        NodeChain                   // A sequence of field accesses.

        NodeCommand                 // An element of a pipeline.

        NodeDot                     // The cursor, dot.


        NodeField      // A field or method name.

        NodeIdentifier // An identifier; always a function name.

        NodeIf         // An if action.

        NodeList       // A list of Nodes.

        NodeNil        // An untyped nil constant.

        NodeNumber     // A numerical constant.

        NodePipe       // A pipeline of commands.

        NodeRange      // A range action.

        NodeString     // A string constant.

        NodeTemplate   // A template invocation action.

        NodeVariable   // A $ variable.

        NodeWith       // A with action.

)

因此,這里有一個示例程序,它遞歸地遍歷模板樹,并查找具有NodeAction類型的節點,即“非控制操作,例如字段評估”。


這個解決方案只是一個演示,一個概念證明,它不能處理所有情況。


func ListTemplFields(t *template.Template) []string {

    return listNodeFields(t.Tree.Root, nil)

}


func listNodeFields(node parse.Node, res []string) []string {

    if node.Type() == parse.NodeAction {

        res = append(res, node.String())

    }


    if ln, ok := node.(*parse.ListNode); ok {

        for _, n := range ln.Nodes {

            res = listNodeFields(n, res)

        }

    }

    return res

}

使用它的示例:


t := template.Must(template.New("cooltemplate").

    Parse(`<h1>{{ .name }} {{ .age }}</h1>`))

fmt.Println(ListTemplFields(t))

輸出(在Go Playground上試試):


[{{.name}} {{.age}}]


查看完整回答
反對 回復 2022-04-20
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

回答進行了一個小優化,也許會有所幫助:)


func listNodeFieldsV2(node parse.Node) []string {

    var res []string

    if node.Type() == parse.NodeAction {

        res = append(res, node.String())

    }

    if ln, ok := node.(*parse.ListNode); ok {

        for _, n := range ln.Nodes {

            res = append(res, listNodeFieldsV2(n)...)

        }

    }

    return res

}


查看完整回答
反對 回復 2022-04-20
?
MMMHUHU

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

我碰巧需要大致相同的代碼。在我的用例中,我們允許用戶在一側創建模板,并輸入map[string]string用于以不同形式呈現的變量。


這是我到目前為止提出的代碼(受 icza 的回答啟發):


// Extract the template vars required from *simple* templates.

// Only works for top level, plain variables. Returns all problematic parse.Node as errors.

func RequiredTemplateVars(t *template.Template) ([]string, []error) {

    var res []string

    var errors []error

    var ln *parse.ListNode

    ln = t.Tree.Root

Node:

    for _, n := range ln.Nodes {

        if nn, ok := n.(*parse.ActionNode); ok {

            p := nn.Pipe

            if len(p.Decl) > 0 {

                errors = append(errors, fmt.Errorf("Node %v not supported", n))

                continue Node

            }

            for _, c := range p.Cmds {

                if len(c.Args) != 1 {

                    errors = append(errors, fmt.Errorf("Node %v not supported", n))

                    continue Node

                }

                if a, ok := c.Args[0].(*parse.FieldNode); ok {

                    if len(a.Ident) != 1 {

                        errors = append(errors, fmt.Errorf("Node %v not supported", n))

                        continue Node

                    }

                    res = append(res, a.Ident[0])

                } else {

                    errors = append(errors, fmt.Errorf("Node %v not supported", n))

                    continue Node

                }


            }

        } else {

            if _, ok := n.(*parse.TextNode); !ok {

                errors = append(errors, fmt.Errorf("Node %v not supported", n))

                continue Node

            }

        }

    }

    return res, errors

}

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


查看完整回答
反對 回復 2022-04-20
  • 3 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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