فهرست منبع

Updated README.md to show how to use templates with same name in different directories.

Vince Yuan 10 سال پیش
والد
کامیت
05da3fa2dc
1فایلهای تغییر یافته به همراه41 افزوده شده و 0 حذف شده
  1. 41 0
      README.md

+ 41 - 0
README.md

@@ -401,6 +401,7 @@ func main() {
 	router.Run(":8080")
 }
 ```
+templates/index.tmpl
 ```html
 <html><h1>
 	{{ .title }}
@@ -408,6 +409,46 @@ func main() {
 </html>
 ```
 
+Using templates with same name in different directories
+
+```go
+func main() {
+	router := gin.Default()
+	router.LoadHTMLGlob("templates/**/*")
+	router.GET("/post/index", func(c *gin.Context) {
+		c.HTML(http.StatusOK, "post/index.tmpl", gin.H{
+			"title": "Posts",
+		})
+	})
+	router.GET("/user/index", func(c *gin.Context) {
+		c.HTML(http.StatusOK, "user/index.tmpl", gin.H{
+			"title": "Users",
+		})
+	})
+	router.Run(":8080")
+}
+```
+templates/post/index.tmpl
+```html
+{{ define "post/index.tmpl" }}
+<html><h1>
+	{{ .title }}
+</h1>
+<p>Using post/index.tmpl</p>
+</html>
+{{ end }}
+```
+templates/user/index.tmpl
+```html
+{{ define "user/index.tmpl" }}
+<html><h1>
+	{{ .title }}
+</h1>
+<p>Using user/index.tmpl</p>
+</html>
+{{ end }}
+```
+
 You can also use your own html template render
 
 ```go