英文:
Unable to load HTML templates from subdirectories with Gin
问题
我正在使用go gin框架加载HTML文件时遇到问题。当我从主函数加载整个模板文件夹时,它无法读取子目录,只能读取文件。
package app
import (
"github.com/gin-gonic/gin"
)
var (
router = gin.Default()
)
func StartApp() {
router.LoadHTMLGlob("templates/*/*.html")
routersMap()
router.Run(":8080")
}
根据项目结构,它应该显示加载的HTML模板如下:
[GIN-debug] Loaded HTML Templates (2):
- layouts/default.html
- layouts/index.html
- pages/index.html
但实际输出显示如下结果:
[GIN-debug] Loaded HTML Templates (3):
- default.html
- index.html
-
它甚至没有显示第二个index.html文件。
项目结构如下:
英文:
I'm having n issue with loading html files with go gin framework </br>
when i loaded the entire templates folder from the main function its not reading the subdirectories and only read files . <br/>
package app
import (
"github.com/gin-gonic/gin"
)
var (
router = gin.Default()
)
func StartApp() {
router.LoadHTMLGlob("templates/*/*.html")
routersMap()
router.Run(":8080")
}
based on the project structure it supposed to show the Loaded HTML Templates as
[GIN-debug] Loaded HTML Templates (2):
- layouts/default.html
- layouts/index.html
- pages/index.html
but actually the output showing this result
[GIN-debug] Loaded HTML Templates (3):
- default.html
- index.html
-
it doesn't even showing the second index.html file <br />
Project Structure <br />
答案1
得分: 2
我也在学习Go语言和使用gin框架。
我也遇到了这个问题,并解决了一些小问题。我将在这里留下来,以防其他人也遇到同样的问题。
文件夹布局:
.
├── controllers
│ ├── controller.go
│ └── gui.go
├── html
│ └── templates
│ ├── homepage
│ │ └── index.html
│ ├── layouts
│ │ ├── footer.html
│ │ ├── header.html
│ │ └── menu.html
│ └── views
│ └── library.html
├── models
│ ├── bookstruct.go
│ └── dbconnection.go
├── server.go
└── test
└── server_test.go
可能需要的部分是这些文件:
- server.go
- gui.go
- library.html
gui.go
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
...
// BooksView display book library
func BooksView(c *gin.Context) {
c.HTML(
http.StatusOK,
"views/library.html",
gin.H{"title": "Books"},
)
}
library.html
{{ define "views/library.html" }}
<!--在此位置嵌入header.html模板-->
{{ template "layouts/header.html" .}}
<h1>Hello Books!</h1>
<div>Est nisi debitis recusandae necessitatibus voluptate ut. Laudantium eligendi nostrum quisquam voluptates harum
exercitationem nam harum. Voluptatem sit assumenda sit alias dolores. Modi autem et temporibus impedit qui numquam.
Esse ut recusandae quos deserunt nihil repellendus. Sint et voluptatem quia quia dolor aut.</div>
<div>Est nisi debitis recusandae necessitatibus voluptate ut. Laudantium eligendi nostrum quisquam voluptates harum
exercitationem nam harum. Voluptatem sit assumenda sit alias dolores. Modi autem et temporibus impedit qui numquam.
Esse ut recusandae quos deserunt nihil repellendus. Sint et voluptatem quia quia dolor aut.</div>
<!--在此位置嵌入footer.html模板-->
{{ template "layouts/footer.html" .}}
{{ end }}
server.go
import (
c "go_first_api/sub_packages/gormrestapi/controllers"
m "go_first_api/sub_packages/gormrestapi/models"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
)
// RunServer will run and create a server using gin
func RunServer() {
...
r := gin.Default()
r.LoadHTMLGlob("sub_packages/gormrestapi/html/templates/**/*")
r.GET("/library", c.BooksView)
...
r.Run(":8088")
}
*注意: main.go在文件夹路径中位于此子目录的上层。
英文:
I am also working on learning go and using gin.
I came across this issue also and ran into a few minor issues that i resolved.
I will leave this here in case it may someone else.
Folder layout:
.
├── controllers
│ ├── controller.go
│ └── gui.go
├── html
│ └── templates
│ ├── homepage
│ │ └── index.html
│ ├── layouts
│ │ ├── footer.html
│ │ ├── header.html
│ │ └── menu.html
│ └── views
│ └── library.html
├── models
│ ├── bookstruct.go
│ └── dbconnection.go
├── server.go
└── test
└── server_test.go
The part that is probably needed are these files:
- server.go
- gui.go
- library.html
gui.go
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
...
// BooksView display book library
func BooksView(c *gin.Context) {
c.HTML(
http.StatusOK,
"views/library.html",
gin.H{"title": "Books"},
)
}
library.html
{{ define "views/library.html" }}
<!--Embed the header.html template at this location-->
{{ template "layouts/header.html" .}}
<h1>Hello Books!</h1>
<div>Est nisi debitis recusandae necessitatibus voluptate ut. Laudantium eligendi nostrum quisquam voluptates harum
exercitationem nam harum. Voluptatem sit assumenda sit alias dolores. Modi autem et temporibus impedit qui numquam.
Esse ut recusandae quos deserunt nihil repellendus. Sint et voluptatem quia quia dolor aut.</div>
<div>Est nisi debitis recusandae necessitatibus voluptate ut. Laudantium eligendi nostrum quisquam voluptates harum
exercitationem nam harum. Voluptatem sit assumenda sit alias dolores. Modi autem et temporibus impedit qui numquam.
Esse ut recusandae quos deserunt nihil repellendus. Sint et voluptatem quia quia dolor aut.</div>
<!--Embed the footer.html template at this location-->
{{ template "layouts/footer.html" .}}
{{ end }}
server.go
import (
c "go_first_api/sub_packages/gormrestapi/controllers"
m "go_first_api/sub_packages/gormrestapi/models"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
)
// RunServer will run and create a server using gin
func RunServer() {
...
r := gin.Default()
r.LoadHTMLGlob("sub_packages/gormrestapi/html/templates/**/*")
r.GET("/library", c.BooksView)
...
r.Run(":8088")
}
NOTE: main.go is above this sub-dir in the folder path.
答案2
得分: 0
请参考gin#html-rendering,在不同目录中使用相同名称的模板templates/**/*
。
英文:
See gin#html-rendering,
Using templates with same name in different directories templates/**/*
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论