无法使用Gin加载子目录中的HTML模板。

huangapple go评论68阅读模式
英文:

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文件。

项目结构如下:

无法使用Gin加载子目录中的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 (
	&quot;github.com/gin-gonic/gin&quot;
)

var (
	router = gin.Default()
)

func StartApp() {
	router.LoadHTMLGlob(&quot;templates/*/*.html&quot;)
	routersMap()
	router.Run(&quot;:8080&quot;)

}

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 />
无法使用Gin加载子目录中的HTML模板。

答案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 (
	&quot;net/http&quot;

	&quot;github.com/gin-gonic/gin&quot;
)
...

// BooksView display book library
func BooksView(c *gin.Context) {
	c.HTML(
		http.StatusOK,
		&quot;views/library.html&quot;,
		gin.H{&quot;title&quot;: &quot;Books&quot;},
	)
}

library.html

{{ define &quot;views/library.html&quot; }}
&lt;!--Embed the header.html template at this location--&gt;
{{ template &quot;layouts/header.html&quot; .}}

&lt;h1&gt;Hello Books!&lt;/h1&gt;

&lt;div&gt;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.&lt;/div&gt;

&lt;div&gt;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.&lt;/div&gt;

&lt;!--Embed the footer.html template at this location--&gt;
{{ template &quot;layouts/footer.html&quot; .}}
{{ end }}

server.go

import (
	c &quot;go_first_api/sub_packages/gormrestapi/controllers&quot;
	m &quot;go_first_api/sub_packages/gormrestapi/models&quot;

	&quot;github.com/gin-gonic/gin&quot;
	&quot;github.com/rs/zerolog&quot;
)

// RunServer will run and create a server using gin
func RunServer() {
    ...
	r := gin.Default()
	r.LoadHTMLGlob(&quot;sub_packages/gormrestapi/html/templates/**/*&quot;)
	r.GET(&quot;/library&quot;, c.BooksView)
    ...

	r.Run(&quot;:8088&quot;)
}

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/**/*.

huangapple
  • 本文由 发表于 2021年8月13日 10:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/68766027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定