gpt4 book ai didi

go - 如何同时使用 LoadHTMLGlob 和 LoadHTMLFiles

转载 作者:行者123 更新时间:2023-12-03 10:07:51 26 4
gpt4 key购买 nike

我想要templates文件夹下不同子目录的分隔符逻辑模板,下面是我的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, "GET", nil},
R{"/logout", auth.Logout, "GET", nil},

R{"/profile/:user/", profile.UserInfo, "GET", nil},
}
}

// 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 并将所有 pathFiles 写入 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...))}

在 gin 中:使用 LoadHTMLFiles(files...)

关于go - 如何同时使用 LoadHTMLGlob 和 LoadHTMLFiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65546093/

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