import "os"
import "html/template"
...
t, _ := template.ParseFiles("login.html")
t.Execute(os.Stdout, data)
...
login.html:
{{ template "header.html" . }}
<form ....>...</form>
{{ template "footer.html" . }}
没有输出,没有错误。
如果我删除这两行 {{ template "..."。 }},我可以看到正在输出的部分。
制作 {{ template "..."需要什么。 }} 工作还是我完全误解了 html/模板?
最佳答案
您需要为将包含其他模板的文件定义一个名称,然后执行该文件。
登录.tmpl
{{define "login"}}
<!doctype html>
<html lang="en">
..
{{template "header" .}}
</body>
</html>
{{end}}
标题.tmpl
{{define "header"}}
whatever
{{end}}
然后你解析这两个文件
t := template.Must(template.ParseFiles("login.tmpl", "header.tmpl"))
// and then execute the template with the defined name
t.ExecuteTemplate(os.Stdout, "login", data)
关于templates - 戈朗 : What's the pre-requisite to use {{ template "partial.html" . }},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281747/