-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
- Loading branch information
Showing
2 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,88 @@ | ||
# multitemplate | ||
This is a custom HTML render to support multi templates | ||
# Multitemplate | ||
|
||
[![Build Status](https://travis-ci.org/gin-contrib/multitemplate.svg)](https://travis-ci.org/gin-contrib/multitemplate) | ||
[![codecov](https://codecov.io/gh/gin-contrib/multitemplate/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/multitemplate) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/multitemplate)]`(https://goreportcard.com/report/github.com/gin-contrib/multitemplate) | ||
[![GoDoc](https://godoc.org/github.com/gin-contrib/multitemplate?status.svg)](https://godoc.org/github.com/gin-contrib/multitemplate) | ||
|
||
This is a custom HTML render to support multi templates, ie. more than one `*template.Template`. | ||
|
||
# Simple example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-contrib/multitemplate" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.HTMLRender = createMyRender() | ||
router.GET("/", func(c *gin.Context) { | ||
c.HTML(200, "index", data) | ||
}) | ||
router.Run(":8080") | ||
} | ||
|
||
func createMyRender() multitemplate.Render { | ||
r := multitemplate.New() | ||
r.AddFromFiles("index", "base.html", "base.html") | ||
r.AddFromFiles("article", "base.html", "article.html") | ||
r.AddFromFiles("login", "base.html", "login.html") | ||
r.AddFromFiles("dashboard", "base.html", "dashboard.html") | ||
|
||
return r | ||
} | ||
``` | ||
|
||
## Advanced example | ||
|
||
[https://elithrar.github.io/article/approximating-html-template-inheritance/](https://elithrar.github.io/article/approximating-html-template-inheritance/) | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"path/filepath" | ||
|
||
"github.com/gin-gonic/contrib/renders/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.tmpl", gin.H{ | ||
"title": "Welcome!", | ||
}) | ||
}) | ||
router.Run(":8080") | ||
} | ||
|
||
func loadTemplates(templatesDir string) multitemplate.Render { | ||
r := multitemplate.New() | ||
|
||
layouts, err := filepath.Glob(templatesDir + "layouts/*.tmpl") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
includes, err := filepath.Glob(templatesDir + "includes/*.tmpl") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Generate our templates map from our layouts/ and includes/ directories | ||
for _, layout := range layouts { | ||
files := append(includes, layout) | ||
r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...))) | ||
} | ||
return r | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package multitemplate | ||
|
||
import ( | ||
"html/template" | ||
|
||
"github.com/gin-gonic/gin/render" | ||
) | ||
|
||
type Render map[string]*template.Template | ||
|
||
var _ render.HTMLRender = Render{} | ||
|
||
func New() Render { | ||
return make(Render) | ||
} | ||
|
||
func (r Render) Add(name string, tmpl *template.Template) { | ||
if tmpl == nil { | ||
panic("template can not be nil") | ||
} | ||
if len(name) == 0 { | ||
panic("template name cannot be empty") | ||
} | ||
r[name] = tmpl | ||
} | ||
|
||
func (r Render) AddFromFiles(name string, files ...string) *template.Template { | ||
tmpl := template.Must(template.ParseFiles(files...)) | ||
r.Add(name, tmpl) | ||
return tmpl | ||
} | ||
|
||
func (r Render) AddFromGlob(name, glob string) *template.Template { | ||
tmpl := template.Must(template.ParseGlob(glob)) | ||
r.Add(name, tmpl) | ||
return tmpl | ||
} | ||
|
||
func (r *Render) AddFromString(name, templateString string) *template.Template { | ||
tmpl := template.Must(template.New("").Parse(templateString)) | ||
r.Add(name, tmpl) | ||
return tmpl | ||
} | ||
|
||
func (r Render) Instance(name string, data interface{}) render.Render { | ||
return render.HTML{ | ||
Template: r[name], | ||
Data: data, | ||
} | ||
} |