Skip to content

Commit

Permalink
initial project.
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Dec 13, 2016
1 parent 26d06d5 commit 6d037bc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
90 changes: 88 additions & 2 deletions README.md
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
}
```
50 changes: 50 additions & 0 deletions multitemplate.go
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,
}
}

0 comments on commit 6d037bc

Please sign in to comment.