如何解决如何同时使用 LoadHTMLGlob 和 LoadHTMLFiles
我想要templates
文件夹下不同子目录的分隔符逻辑模板,以下是我的模板文件夹
templates
├── authentication
│ ├── login.gohtml
│ └── logout.gohtml
├── index.gohtml
└── profile
└── userinfo.gohtml
这是main.go
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.New()
// router.Use(Threshold()) // not finished yet
router.Use(Cors())
// load templates
// router.LoadHTMLGlob("templates/authentication/*.gohtml")
router.LoadHTMLGlob("./templates/*/*.gohtml")
router.LoadHTMLGlob("./templates/*.gohtml")
// set static folder
router.Static("/static","./public")
// handler the favicon.ico
router.StaticFile("/favicon.ico","./public/images/favicon.ico")
InjectRoutes(router)
// run the server at default port 8080
router.Run(":8080")
}
InjectRoutes 来自 route.go
package main
import (
"github.com/gin-gonic/gin"
auth "github.com/yangwawa0323/mywebapp/v2/authentication"
"github.com/yangwawa0323/mywebapp/v2/profile"
)
// Route struct
type Route struct {
path string
handler gin.HandlerFunc
method string
templateFile interface{}
}
// R is alias of `Route`
type R = Route
// GenerateRoutes generate all the routes.
func GenerateRoutes() []Route {
return []Route{
R{"/",HomePage,"GET","./templates/index.gohtml"},R{"/login",auth.Login,nil},R{"/logout",auth.Logout,R{"/profile/:user/",profile.UserInfo,}
}
// InjectRoutes function inject routes to gin.Engine
func InjectRoutes(router *gin.Engine) {
for _,r := range GenerateRoutes() {
if r.templateFile != nil {
router.LoadHTMLFiles(r.templateFile.(string))
}
switch r.method {
case "POST":
router.POST(r.path,r.handler)
case "DELETE":
router.DELETE(r.path,r.handler)
case "PUT":
router.PUT(r.path,r.handler)
default:
router.GET(r.path,r.handler)
}
}
}
这里是调试信息
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] Loaded HTML Templates (4):
-
- login.gohtml
- logout.gohtml
- userinfo.gohtml
[GIN-debug] Loaded HTML Templates (2):
- index.gohtml
-
2021/01/03 10:02:34 http: panic serving [::1]:50590: html/template: "userinfo.gohtml" is undefined
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc000302000)
/usr/lib/go-1.13/src/net/http/
如你所见,已经加载了模板,但 LoadHTMLGlod 和 LoadHTMLFiles 只有一个在工作,最后一个优先于前一个。
我的问题是如何编写 glob 来实现我的要求或具有编写代码的其他技能,我不想创建一个新目录并将 index.gohtml
放入其中,即使它正在工作?谢谢。
解决方法
LoadHTMLGlob("path_patren")
它无法匹配您的所有文件路径。因为您的文件存储在不同的深度。并且不在同一深度。
请参阅 filepath.Glob 的文档。详情请见。
如您所知:调用 loadHTMLGlob()
并不能解决问题。您实际上是再次重置模板并清除您在第一次调用该函数时获得的设置。
解决方法很简单。
只需删除 loadHTMLGlob 并在 LoadHTMLFiles 中写入所有路径文件。
像这样:
files := []string{
"home.html","acount.html","login.html","sign.html","folder1/stores.html","folder1/mystore.html","folder1/upload.html","folder2/templs/products.html","foleder2/partial/header.html","tmpl/partial/footer.html"}
templ := &Template{templates: template.Must(template.ParseFiles(files...))}
在杜松子酒中:使用 LoadHTMLFiles(files...)
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。