如何从已解析的模板中获取模板“操作”的地图或列表?

所以我想以某种方式将{{ .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"})

}

有没有办法像这样检查已解析的模板?

谢谢!


HUH函数
浏览 128回答 3
3回答

富国沪深

前言:正如 Voker 所建议的,该Template.Tree字段“仅导出供 html/template 使用,应被所有其他客户端视为未导出”。您不应该依赖这样的东西来为模板执行提供输入。您必须知道要执行的模板及其预期的数据。你不应该在运行时“探索”它来为它提供参数。您从解析模板中获得的价值是template.Template(text/template或者html/template,它们具有相同的 API)。此模板将模板表示为类型树parse.Tree。文本模板包含的所有内容都存储在此树中的节点中,包括静态文本、动作等。话虽如此,您可以遍历这棵树并查找标识访问字段或调用函数的此类操作的节点。节点的类型parse.Node具有Node.Type()返回其类型的方法。可能的类型在包中定义为常量,parse在类型旁边parse.NodeType,例如const (&nbsp; &nbsp; &nbsp; &nbsp; NodeText&nbsp; &nbsp; NodeType = iota // Plain text.&nbsp; &nbsp; &nbsp; &nbsp; NodeAction&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // A non-control action such as a field evaluation.&nbsp; &nbsp; &nbsp; &nbsp; NodeBool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // A boolean constant.&nbsp; &nbsp; &nbsp; &nbsp; NodeChain&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// A sequence of field accesses.&nbsp; &nbsp; &nbsp; &nbsp; NodeCommand&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// An element of a pipeline.&nbsp; &nbsp; &nbsp; &nbsp; NodeDot&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// The cursor, dot.&nbsp; &nbsp; &nbsp; &nbsp; NodeField&nbsp; &nbsp; &nbsp; // A field or method name.&nbsp; &nbsp; &nbsp; &nbsp; NodeIdentifier // An identifier; always a function name.&nbsp; &nbsp; &nbsp; &nbsp; NodeIf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// An if action.&nbsp; &nbsp; &nbsp; &nbsp; NodeList&nbsp; &nbsp; &nbsp; &nbsp;// A list of Nodes.&nbsp; &nbsp; &nbsp; &nbsp; NodeNil&nbsp; &nbsp; &nbsp; &nbsp; // An untyped nil constant.&nbsp; &nbsp; &nbsp; &nbsp; NodeNumber&nbsp; &nbsp; &nbsp;// A numerical constant.&nbsp; &nbsp; &nbsp; &nbsp; NodePipe&nbsp; &nbsp; &nbsp; &nbsp;// A pipeline of commands.&nbsp; &nbsp; &nbsp; &nbsp; NodeRange&nbsp; &nbsp; &nbsp; // A range action.&nbsp; &nbsp; &nbsp; &nbsp; NodeString&nbsp; &nbsp; &nbsp;// A string constant.&nbsp; &nbsp; &nbsp; &nbsp; NodeTemplate&nbsp; &nbsp;// A template invocation action.&nbsp; &nbsp; &nbsp; &nbsp; NodeVariable&nbsp; &nbsp;// A $ variable.&nbsp; &nbsp; &nbsp; &nbsp; NodeWith&nbsp; &nbsp; &nbsp; &nbsp;// A with action.)因此,这里有一个示例程序,它递归地遍历模板树,并查找具有NodeAction类型的节点,即“非控制操作,例如字段评估”。这个解决方案只是一个演示,一个概念证明,它不能处理所有情况。func ListTemplFields(t *template.Template) []string {&nbsp; &nbsp; return listNodeFields(t.Tree.Root, nil)}func listNodeFields(node parse.Node, res []string) []string {&nbsp; &nbsp; if node.Type() == parse.NodeAction {&nbsp; &nbsp; &nbsp; &nbsp; res = append(res, node.String())&nbsp; &nbsp; }&nbsp; &nbsp; if ln, ok := node.(*parse.ListNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, n := range ln.Nodes {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = listNodeFields(n, res)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return res}使用它的示例:t := template.Must(template.New("cooltemplate").&nbsp; &nbsp; Parse(`<h1>{{ .name }} {{ .age }}</h1>`))fmt.Println(ListTemplFields(t))输出(在Go Playground上试试):[{{.name}} {{.age}}]

繁花如伊

回答进行了一个小优化,也许会有所帮助:)func listNodeFieldsV2(node parse.Node) []string {&nbsp; &nbsp; var res []string&nbsp; &nbsp; if node.Type() == parse.NodeAction {&nbsp; &nbsp; &nbsp; &nbsp; res = append(res, node.String())&nbsp; &nbsp; }&nbsp; &nbsp; if ln, ok := node.(*parse.ListNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; for _, n := range ln.Nodes {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = append(res, listNodeFieldsV2(n)...)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return res}

MMMHUHU

我碰巧需要大致相同的代码。在我的用例中,我们允许用户在一侧创建模板,并输入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) {&nbsp; &nbsp; var res []string&nbsp; &nbsp; var errors []error&nbsp; &nbsp; var ln *parse.ListNode&nbsp; &nbsp; ln = t.Tree.RootNode:&nbsp; &nbsp; for _, n := range ln.Nodes {&nbsp; &nbsp; &nbsp; &nbsp; if nn, ok := n.(*parse.ActionNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p := nn.Pipe&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(p.Decl) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors = append(errors, fmt.Errorf("Node %v not supported", n))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue Node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _, c := range p.Cmds {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(c.Args) != 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors = append(errors, fmt.Errorf("Node %v not supported", n))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue Node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if a, ok := c.Args[0].(*parse.FieldNode); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(a.Ident) != 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors = append(errors, fmt.Errorf("Node %v not supported", n))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue Node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = append(res, a.Ident[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors = append(errors, fmt.Errorf("Node %v not supported", n))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue Node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _, ok := n.(*parse.TextNode); !ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errors = append(errors, fmt.Errorf("Node %v not supported", n))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue Node&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return res, errors}https://play.golang.org/p/nH95B45jUmI
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go