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}}]

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
}

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
- 3 回答
- 0 關注
- 145 瀏覽
添加回答
舉報