gpt4 book ai didi

templates - template.ParseGlob() 可以解析子目录中的模板吗?

转载 作者:IT王子 更新时间:2023-10-29 01:39:28 28 4
gpt4 key购买 nike

要清理模板文件夹,我想将常用模板保存在子文件夹中。目前我有以下文件结构:

main.go
templates/index.tpl # Main template for the main page
templates/includes/head.tpl
templates/includes/footer.tpl

head.tplfooter.tpl 将在 index.tpl 中调用,如下所示:

{{ template "head" . }}
<h1>My content</h1>
{{ 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 模式忽略?我找到了 this类似的问题,但答案仅提供一种解决方法。

最佳答案

不,它不能。

此处的文档非常清楚:template.ParseGlob 的 Globbing 工作方式与 filepath.Glob 类似,filepath.Glob 使用语法filepath.Match ( https://godoc.org/path/filepath#Match ) 没有用于深度匹配的 **

(仔细阅读文档真的很有帮助。)

关于templates - template.ParseGlob() 可以解析子目录中的模板吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32309830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com