html/template 完全指南:怎么用、为什么用、用完效果 + New/ParseFiles/Must/Execute

html/template 完全指南:怎么用、为什么用、用完效果 + New/ParseFiles/Must/Execute
个人主页会编程的土豆欢迎来访作者简介后端学习者❄️个人专栏数据结构与算法数据库leetcode✨那些你一个人走过的夜路终将化作照亮未来的光一、怎么使用三步第 1 步准备带「坑位」的 HTML{{if .IsLogin}} 欢迎 {{.Username}} {{else}} a href/login登录/a {{end}} {{range .Books}} div{{.Title}} — {{.Price}}/div {{end}}写法含义{{.Username}}取传入数据的字段{{if .IsLogin}}条件显示{{range .Books}}循环列表第 2 步处理器里加载模板import html/template t : template.Must(template.ParseFiles(views/index.html))第 3 步塞数据并写入响应t.Execute(w, page)登录失败示例t : template.Must(template.ParseFiles(views/pages/user/login.html)) t.Execute(w, 用户名或密码不正确) // 模板里 {{.}} 显示这句背这套公式即可t : template.Must(template.ParseFiles(某个.html)) t.Execute(w, 数据)二、为什么使用不用模板会怎样html : htmlbody for _, b : range books { html div b.Title /div } html /body/html w.Write([]byte(html))页面一复杂就又臭又长、难维护。用模板解决什么原因说明HTML 与 Go 分离样子在views逻辑在controller数据驱动页面几本书显示几本登录/未登录两套导航课与项目习惯尚硅谷「给客户端响应」的主流做法何时用有结构化数据要回完整网页。何时可不使用Ajax 只回一句字或FileServer纯静态文件。三、使用完了有什么效果Execute(w, data)之后HTTP 响应体里已经是生成好的 HTML浏览器渲染成整页首页、登录成败页、图书列表……用户看到的是动态页面书名、欢迎语来自数据库/Session写法效果t.Execute(w, page)整页书城界面fmt.Fprintln(w, ok)往往只有一行字Ajaxw.Write(...)不整页跳转只改一个提示比喻模板 填空卷Execute 交卷给浏览器效果 用户看到填好空的网页。四、在 Web 链路里的位置处理请求读 r→ dao 查库 → 【模板 Parse Execute】→ 浏览器显示整页 HTML和 Ajax 对比方式典型代码浏览器感觉模板整页t.Execute(w, page)一整页 HTMLAjax 小段w.Write([]byte(用户名已存在))只改一个提示书城首页、登录、注册、图书管理 ——主路径几乎全是模板整页。官方 API 与书城细讲老师打开文档时常见New、ParseFiles、ParseGlob、Must、Name、Delims……核心一句用某种方式得到*template.Template再Execute(w, data)填数据写响应。五、最小可运行例子func hello(w http.ResponseWriter, r *http.Request) { t : template.Must(template.ParseFiles(hello.html)) t.Execute(w, 张三) }hello.htmlh1你好{{.}}/h1→ 页面显示「你好张三」。书城只是把文件换成views/index.html数据换成page。六、API 逐个讲人话分类① 创建/加载 ② 辅助 ③ 输出响应Execute6.1New(name)—— 建空壳t : template.New(myPage) // 还没有 HTML t, err t.Parse(h1{{.}}/h1)课上演示字符串模板时用书城文件模板可直接ParseFiles不必先New。6.2Parse(text)—— 解析字符串在已有模板上塞入 HTML 字符串。常与New连用template.Must(template.New(name).Parse(html字符串))6.3ParseFiles(...)★书城主力t : template.Must(template.ParseFiles(views/index.html))从磁盘读文件并解析至少传一个路径路径相对go run的当前目录一般是项目根登录成败t : template.Must(template.ParseFiles(views/pages/user/login_success.html)) t.Execute(w, user) t : template.Must(template.ParseFiles(views/pages/user/login.html)) t.Execute(w, 用户名或密码不正确)6.4ParseGlob(views/*.html)按通配符批量加载。入门可不用会单文件ParseFiles即可。6.5Must(t, err)★几乎必用err ! nil就 panic否则返回t。少写错误处理路径错了会直接崩方便发现。6.6Name()/Delims()Name查模板名字调试用Delims改{{ }}分隔符书城不用6.7Execute(w, data)★处理响应的关键t.Execute(w, page)用 data 填{{.}}/{{range}}…写入w即响应体只 Parse 不 Execute浏览器什么都没有Parse* 准备模板Execute 真正输出。七、API 怎么选写法何时用书城NewParse字符串演示少ParseFiles具体 html 文件主力ParseGlob批量加载可选Must包住加载函数常用Execute写入响应必须Name/Delims调试/改符号可忽略八、模板语法要点点.Execute的第二个参数在模板根上就是.。传入模板错误提示{{.}}user{{.Username}}page{{.Books}}{{.PageNo}}字段须首字母大写导出才能访问。if/range课里「动作」{{if .IsLogin}}欢迎 {{.Username}}{{else}}请登录{{end}} {{range .Books}} {{.Title}} {{.Price}} {{end}}处理器先设page.IsLogin、page.Username再Execute(w, page)。range里.变成当前那本书。其它包含、define/block跟课练即可。九、书城对照首页t : template.Must(template.ParseFiles(views/index.html)) t.Execute(w, page) // 含 Books、IsLogin、分页登录请求 响应都在username : r.PostFormValue(username) password : r.PostFormValue(password) // …查库… t.Execute(w, user) // 或 t.Execute(w, 用户名或密码不正确)静态页 vs 模板页访问方式动态错误提示/pages/user/login.htmlFileServer难/login 模板Execute能十文档人话New建空模板壳Parse塞 HTML 字符串ParseFiles从文件加载常用ParseGlob通配符批量加载Must失败就 panicName问名字Delims改{{ }}Execute填数据写响应← 没有它前面白学十一、易错点只 Parse 忘 Execute → 空白路径错 Must → panic在项目根运行字段小写模板取不到data 传错类型回 HTML 用html/template不是text/templateCookie 等要在Execute 之前SetCookie以为必须会齐 New/ParseGlob —— 书城会公式即可