templates - 如何从已解析的模板中获取模板 'actions' 的 map 或列表?

标签 templates go go-templates

所以我想以某种方式将模板中定义的所有 {{ .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"})
}

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

最佳答案

前言:正如 Voker 所建议的,Template.Tree 字段是“仅供 html/模板使用而导出,所有其他客户端应视为未导出。”

你不应该依赖这样的东西来为模板执行提供输入。您必须知道要执行的模板及其期望的数据。您不应该在运行时“探索”它来为其提供参数。


解析模板得到的值是 template.Template (text/templatehtml/template,它们具有相同的 API)。此模板将模板表示为 parse.Tree 类型的树.文本模板包含的所有内容都存储在这棵树的节点中,包括静态文本、 Action 等。

话虽如此,您可以遍历这棵树并查找标识此类访问字段或调用函数的操作的节点。节点类型为 parse.Node它有一个返回其类型的 Node.Type() 方法。可能的类型在 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}}]

关于templates - 如何从已解析的模板中获取模板 'actions' 的 map 或列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52585977/

相关文章:

以函数作为模板参数的 C++ 函数调用包装器

c++ - 为什么我们在使用宏时找不到合适的运算符重载?

模板类中的 C++ 枚举不能用作常量值

javascript - 在 Javascript 中引用 Go 数组

templates - 如何使用特定键访问该结构映射中的结构字段值

go - 如何在Go模板中修剪空白

c++ - 在不知道参数的情况下传递模板

http - 在 Google App Engine 上对封闭主体的读取无效

Golang 导出类型

json - 如何使用mongo-go-driver有效地将bson转换为json?