template.ParseGlob() 可以解析子目录中的模板吗?
来源:Golang技术栈
时间:2023-03-04 17:01:13 422浏览 收藏
本篇文章给大家分享《template.ParseGlob() 可以解析子目录中的模板吗?》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
问题内容
要清理模板文件夹,我想将常用模板保存在子文件夹中。目前我有以下文件结构:
main.go templates/index.tpl # Main template for the main page templates/includes/head.tpl templates/includes/footer.tpl
head.tpl并将footer.tpl在内部调用index.tpl,如下所示:
{{ template "head" . }}
My content
{{ template "footer" .}}
此外,文件使用template.ParseGlob(). 以下是摘录main.go:
var views = template.Must(template.ParseGlob("src/templates/**/*"))
func Render(rw http.ResponseWriter, temp string, data interface{}) {
err := views.ExecuteTemplate(rw, temp, data)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
func Index(rw http.ResponseWriter, req *http.Request) {
Render(rw, "index.tpl", nil)
}
每次打开浏览器时,都会收到以下错误消息:html/template: "index.tpl" is undefined.
有没有可能,这index.tpl被这个 glob 模式忽略了?我发现[了这个](http://grokbase.com/t/gg/golang-
nuts/137tj3axkn/go-nuts-template-parseglob-pattern-to-parse-all-files-in-a-
directory-recursively)类似的问题,但答案只是一种解决方法。
正确答案
不,它不能。
这里的文档很清楚: Globbing for template.ParseGlobworks like in filepath.Globand
filepath.Globuses the syntax of filepath.Match(
https://godoc.org/path/filepath#Match
) which has no **for a deep match.
(仔细阅读文档 真的很有帮助。)
本篇关于《template.ParseGlob() 可以解析子目录中的模板吗?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
-
193 收藏
-
354 收藏
-
418 收藏
-
161 收藏
-
209 收藏
-
246 收藏
-
251 收藏
-
Golang · Go问答 | 6小时前 | 依赖注入 · 设计模式 · api设计 · Go问答 · Functional Options · API设计 默认值 依赖注入 Go问答 Functional Options 函数选项153 收藏
-
Golang · Go问答 | 6小时前 | 单元测试 · HTTP · Go问答 · httptest · ResponseRecorder · 单元测试 HTTP响应 httptest Go问答 ResponseRecorder Handler测试481 收藏
-
Golang · Go问答 | 6小时前 | 并发 · 性能优化 · sync.Pool · bytes.Buffer · Go问答 · reset bytes.Buffer 对象复用 Go sync.Pool 数据串行294 收藏
-
Golang · Go问答 | 11小时前 | golang · errgroup · Context · 并发控制 · Go问答 · SetLimit · 错误传播 Go errgroup.SetLimit errgroup.WithContext Go 并发限制 批量调用 context 取消467 收藏
-
Golang · Go问答 | 12小时前 | WaitGroup · golang · goroutine · 并发编程 · Go问答 · Go 1.25 · Goroutine 并发 WaitGroup Go 1.25 sync.WaitGroup.Go Add Done448 收藏
-
392 收藏
-
349 收藏
-
Golang · Go问答 | 1天前 | 云原生 · golang · 性能优化 · kubernetes · HPA · Go HPA不扩容 Kubernetes HPA CPU resources requests HPA并发指标 Go服务扩缩容111 收藏
-
Golang · Go问答 | 1天前 | golang · 消息队列 · 性能优化 · websocket · 高并发 · Go WebSocket广播 Go慢客户端 WebSocket发送队列 Go Hub分片 WebSocket背压146 收藏
-
Golang · Go问答 | 1天前 | 性能排查 · net/http · HTTP客户端 · Go问答 · 连接复用 · 连接复用 keep-alive http.Transport Response.Body Go HTTP Client241 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习