我试图通过子模板输出变量
它输出My name is Mary and I'm <no value> years old.
而不是My name is Mary and I'm 35 years old.
以下是代码:
server.go
:
package main
import (
"fmt"
"os"
"text/template"
)
type Person struct {
Name, Age string
}
func main() {
p := Person{Name: "Mary", Age: "35"}
templatestuff, _ := template.ParseFiles("t1.tmpl", "t2.tmpl")
templatestuff.ExecuteTemplate(os.Stdout, "basetemplate", p) //execute basetemplate
fmt.Println()
templatestuff.Execute(os.Stdout, p)
}
t1.tmpl
:
{{define "basetemplate"}}My name is {{.Name}} and {{template "insideit"}}years old.{{end}}
t2.tmpl
:
{{define "insideit"}}I'm {{.Age}} {{end}}
看起来我错过了什么。