templates - 戈朗 : What's the pre-requisite to use {{ template "partial.html" . }}

标签 templates go

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/

相关文章:

bash - 只为 Mac 创建和运行 go build?

database - 使用 go 和 InstantClient 从存储过程访问 OUT 参数(t_cursor 类型)

unit-testing - 您如何为连接到授权帐户的库编写测试?

c++ - 结构相同的专业

c++ - 模板模板函数的重载

c++ - 如何在 C++ 中使用泛型函数?

c++ - 为容器内保存的指针和非指针类型数据创建模板函数重载

go - 从列表的大小循环,但在goroutine中未调用某些索引

c++ - 事件类需要更新

mongodb - 查询有关 Go 项目中对象数组的 MongoDb 集合