From 05da3fa2dc473d7cce53fcf4490a586404859d87 Mon Sep 17 00:00:00 2001 From: Vince Yuan Date: Tue, 2 Jun 2015 23:06:08 +0800 Subject: [PATCH 1/2] Updated README.md to show how to use templates with same name in different directories. --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index 01955567..de79ec16 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,7 @@ func main() { router.Run(":8080") } ``` +templates/index.tmpl ```html

{{ .title }} @@ -408,6 +409,46 @@ func main() { ``` +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" }} +

+ {{ .title }} +

+

Using post/index.tmpl

+ +{{ end }} +``` +templates/user/index.tmpl +```html +{{ define "user/index.tmpl" }} +

+ {{ .title }} +

+

Using user/index.tmpl

+ +{{ end }} +``` + You can also use your own html template render ```go From 7229c488e80e33fa1bedf1a02a4584a4cd166d38 Mon Sep 17 00:00:00 2001 From: Vince Yuan Date: Tue, 2 Jun 2015 23:12:00 +0800 Subject: [PATCH 2/2] Use plural for template folder name in the example. --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index de79ec16..8b655b99 100644 --- a/README.md +++ b/README.md @@ -415,36 +415,36 @@ Using templates with same name in different directories 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{ + router.GET("/posts/index", func(c *gin.Context) { + c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "Posts", }) }) - router.GET("/user/index", func(c *gin.Context) { - c.HTML(http.StatusOK, "user/index.tmpl", gin.H{ + router.GET("/users/index", func(c *gin.Context) { + c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "Users", }) }) router.Run(":8080") } ``` -templates/post/index.tmpl +templates/posts/index.tmpl ```html -{{ define "post/index.tmpl" }} +{{ define "posts/index.tmpl" }}

{{ .title }}

-

Using post/index.tmpl

+

Using posts/index.tmpl

{{ end }} ``` -templates/user/index.tmpl +templates/users/index.tmpl ```html -{{ define "user/index.tmpl" }} +{{ define "users/index.tmpl" }}

{{ .title }}

-

Using user/index.tmpl

+

Using users/index.tmpl

{{ end }} ```