点击上方蓝字,后台回复【合集】获取 Go资料
介绍
为什么我会选择 Go?
使用Go渲染HTML
import "html/template"
tmpl, _ := template.New("").Parse("<h1>{{.}}</h1>") // from string
tmpl, _ := template.ParseFiles("demo.html") // from file
tmpl.Execute(w, "Hello world")
// Output: <h1>Hello world</h1>
我如何构建我的模板?
当谈到 Go 时,如何处理模板片段(例如页眉、内容、页脚)有两种可能性。
1.WordPress的方式 2.Django、Rails、Laravel 的方式
WordPress 方式(不要这样做)
<!-- header.html -->
{{define "header"}}
<html>
<body>
<div class="navbar">...</div>
<div class="content">
{{end}}
<!-- profile.html -->
{{template "header" .}}
<div class="profile">
Your username: {{.User.Name}}
</div>
{{template "footer" .}}
<!-- footer.html -->
{{define "footer"}}
</div>
</body>
</html>
{{end}}
tmpl, _ := template.ParseFiles("header.html", "footer.html", "profile.html")
tmpl.Execute(w, User{Name: "philippta"})
Django、Rails、Laravel 方式(这样做)
<!-- layout.html -->
<html>
<body>
<div class="navbar">...</div>
<div class="content">
{{block "content" .}}
<!-- Fallback, if "content" is not defined elsewhere -->
{{end}}
</div>
</body>
</html>
<!-- profile.html -->
{{define "content"}}
<div class="profile">
Your username: {{.User.Name}}
</div>
{{end}}
相应的 Go 代码如下所示:
tmpl, _ := template.ParseFiles("layout.html", "profile.html")
tmpl.Execute(w, User{Name: "philippta"})
<!-- layout.html -->
<html>
<head>
<title>{{block "title"}}Default page title{{end}}</title>
<script src="app.js"></script>
{{block "additional_scripts"}}{{end}}
</head>
<body>
<div class="navbar">...</div>
<div class="content">
{{block "content" .}}
<!-- Fallback, if "content" is not defined elsewhere -->
{{end}}
</div>
</body>
</html>
实现模板渲染器
封装结构
html/
layout.html
dashboard.html
profile/
show.html
edit.html
html.go <-- here are the rendering functions
main.go <-- here could be the http handlers
捆绑模板
import "embed"
//go:embed *
var files embed.FS
解析模板
import "html/template"
func parse(file string) *template.Template {
return template.Must(
template.New("layout.html").ParseFS(files, "layout.html", file))
}
var funcs := template.FuncMap{
"uppercase": func(v string) string {
return strings.ToUpper(v)
},
}
func parse(file string) *template.Template {
return template.Must(
- template.New("layout.html").ParseFS(files, "layout.html", file))
+ template.New("layout.html").Funcs(funcs).ParseFS(files, "layout.html", file))
}
var (
dashboard = parse("dashboard.html")
profileShow = parse("profile/show.html")
profileEdit = parse("profile/edit.html")
)
模板辅助结构和函数
type DashboardParams struct {
User User
Statistics []Statistics
}
func Dashboard(w io.Writer, p DashboardParams) error {
return dashboard.Execute(w, p)
}
type ProfileShowParams struct {
User User
ProfileInfo Profile
}
func ProfileShow(w io.Writer, p ProfileShowParams) error {
return profileShow.Execute(w, p)
}
// and so on ...
使用新的模板函数
import "github.com/philippta/myproject/html"
http.HandleFunc("/dashboard", func(w http.ResponseWriter, r *http.Request) {
user := getUser()
stats := getStatistics()
params := html.DashboardParams{
User: user,
Statistics: stats,
}
html.Dashboard(w, params)
})
包起来
来源:https://philipptanlak.com/web-frontends-in-go/,侵删