From 41d1d62d1df32090eb009f4741d9eef6feff6967 Mon Sep 17 00:00:00 2001 From: Sai Date: Thu, 7 Jun 2018 11:41:23 +0900 Subject: [PATCH] Add advanced example source code (#15) Hi! From https://github.com/gin-contrib/multitemplate/issues/14, I think it's difficult to understand advanced example in readme. So that I wrote a working advanced example. Thanks. --- README.md | 26 +++++++---- example/advanced/example.go | 46 +++++++++++++++++++ .../advanced/templates/includes/article.html | 1 + .../advanced/templates/includes/index.html | 1 + example/advanced/templates/layouts/base.html | 3 ++ example/{ => simple}/example.go | 0 example/{ => simple}/templates/article.html | 0 example/{ => simple}/templates/base.html | 0 example/{ => simple}/templates/index.html | 0 9 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 example/advanced/example.go create mode 100644 example/advanced/templates/includes/article.html create mode 100644 example/advanced/templates/includes/index.html create mode 100644 example/advanced/templates/layouts/base.html rename example/{ => simple}/example.go (100%) rename example/{ => simple}/templates/article.html (100%) rename example/{ => simple}/templates/base.html (100%) rename example/{ => simple}/templates/index.html (100%) diff --git a/README.md b/README.md index faa0a52..544f919 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ import "github.com/gin-contrib/multitemplate" ### Simple example -See [example/example.go](example/example.go) +See [example/simple/example.go](example/simple/example.go) -[embedmd]:# (example/example.go go) +[embedmd]:# (example/simple/example.go go) ```go package main @@ -64,11 +64,13 @@ func main() { [Approximating html/template Inheritance](https://elithrar.github.io/article/approximating-html-template-inheritance/) +See [example/advanced/example.go](example/advanced/example.go) + +[embedmd]:# (example/advanced/example.go go) ```go package main import ( - "html/template" "path/filepath" "github.com/gin-contrib/multitemplate" @@ -79,30 +81,36 @@ func main() { router := gin.Default() router.HTMLRender = loadTemplates("./templates") router.GET("/", func(c *gin.Context) { - c.HTML(200, "index.tmpl", gin.H{ + c.HTML(200, "index.html", gin.H{ "title": "Welcome!", }) }) + router.GET("/article", func(c *gin.Context) { + c.HTML(200, "article.html", gin.H{ + "title": "Html5 Article Engine", + }) + }) + router.Run(":8080") } func loadTemplates(templatesDir string) multitemplate.Renderer { r := multitemplate.NewRenderer() - layouts, err := filepath.Glob(templatesDir + "/layouts/*.tmpl") + layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") if err != nil { panic(err.Error()) } - includes, err := filepath.Glob(templatesDir + "/includes/*.tmpl") + includes, err := filepath.Glob(templatesDir + "/includes/*.html") if err != nil { panic(err.Error()) } // Generate our templates map from our layouts/ and includes/ directories - for _, layout := range layouts { - files := append([]string{layout}, includes...) - r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...))) + for _, include := range includes { + files := append(layouts, include) + r.AddFromFiles(filepath.Base(include), files...) } return r } diff --git a/example/advanced/example.go b/example/advanced/example.go new file mode 100644 index 0000000..8ce279d --- /dev/null +++ b/example/advanced/example.go @@ -0,0 +1,46 @@ +package main + +import ( + "path/filepath" + + "github.com/gin-contrib/multitemplate" + "github.com/gin-gonic/gin" +) + +func main() { + router := gin.Default() + router.HTMLRender = loadTemplates("./templates") + router.GET("/", func(c *gin.Context) { + c.HTML(200, "index.html", gin.H{ + "title": "Welcome!", + }) + }) + router.GET("/article", func(c *gin.Context) { + c.HTML(200, "article.html", gin.H{ + "title": "Html5 Article Engine", + }) + }) + + router.Run(":8080") +} + +func loadTemplates(templatesDir string) multitemplate.Renderer { + r := multitemplate.NewRenderer() + + layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") + if err != nil { + panic(err.Error()) + } + + includes, err := filepath.Glob(templatesDir + "/includes/*.html") + if err != nil { + panic(err.Error()) + } + + // Generate our templates map from our layouts/ and includes/ directories + for _, include := range includes { + files := append(layouts, include) + r.AddFromFiles(filepath.Base(include), files...) + } + return r +} diff --git a/example/advanced/templates/includes/article.html b/example/advanced/templates/includes/article.html new file mode 100644 index 0000000..25eeb21 --- /dev/null +++ b/example/advanced/templates/includes/article.html @@ -0,0 +1 @@ +{{define "content"}}Hi, this is article template{{end}} diff --git a/example/advanced/templates/includes/index.html b/example/advanced/templates/includes/index.html new file mode 100644 index 0000000..33794f5 --- /dev/null +++ b/example/advanced/templates/includes/index.html @@ -0,0 +1 @@ +{{define "content"}}Hi, this is index.html{{end}} diff --git a/example/advanced/templates/layouts/base.html b/example/advanced/templates/layouts/base.html new file mode 100644 index 0000000..9333d6d --- /dev/null +++ b/example/advanced/templates/layouts/base.html @@ -0,0 +1,3 @@ +Title: {{ .title }} + +index template: {{template "content" .}} diff --git a/example/example.go b/example/simple/example.go similarity index 100% rename from example/example.go rename to example/simple/example.go diff --git a/example/templates/article.html b/example/simple/templates/article.html similarity index 100% rename from example/templates/article.html rename to example/simple/templates/article.html diff --git a/example/templates/base.html b/example/simple/templates/base.html similarity index 100% rename from example/templates/base.html rename to example/simple/templates/base.html diff --git a/example/templates/index.html b/example/simple/templates/index.html similarity index 100% rename from example/templates/index.html rename to example/simple/templates/index.html