...

Text file src/github.com/gin-gonic/contrib/renders/multitemplate/README.md

Documentation: github.com/gin-gonic/contrib/renders/multitemplate

     1## EOL-warning
     2
     3**This package has been abandoned on 2016-12-13. Please use [gin-contrib/multitemplate](https://github.com/gin-contrib/multitemplate) instead.**
     4
     5This is a custom HTML render to support multi templates, ie. more than one `*template.Template`.
     6
     7#Simple example
     8
     9```go
    10package main
    11
    12import (
    13    "html/template"
    14
    15    "github.com/gin-gonic/gin"
    16    "github.com/gin-gonic/contrib/renders/multitemplate"
    17)
    18
    19func main() {
    20    router := gin.Default()
    21    router.HTMLRender = createMyRender()
    22    router.GET("/", func(c *gin.Context) {
    23        c.HTML(200, "index", data)
    24    })
    25    router.Run(":8080")
    26}
    27
    28func createMyRender() multitemplate.Render {
    29    r := multitemplate.New()
    30    r.AddFromFiles("index", "base.html", "base.html")
    31    r.AddFromFiles("article", "base.html", "article.html")
    32    r.AddFromFiles("login", "base.html", "login.html")
    33    r.AddFromFiles("dashboard", "base.html", "dashboard.html")
    34
    35    return r
    36}
    37```
    38
    39##Advanced example
    40
    41[https://elithrar.github.io/article/approximating-html-template-inheritance/](https://elithrar.github.io/article/approximating-html-template-inheritance/)
    42
    43```go
    44package main
    45
    46import (
    47	"html/template"
    48	"path/filepath"
    49
    50	"github.com/gin-gonic/contrib/renders/multitemplate"
    51	"github.com/gin-gonic/gin"
    52)
    53
    54func main() {
    55	router := gin.Default()
    56	router.HTMLRender = loadTemplates("./templates")
    57	router.GET("/", func(c *gin.Context) {
    58		c.HTML(200, "index.tmpl", gin.H{
    59			"title": "Welcome!",
    60		})
    61	})
    62	router.Run(":8080")
    63}
    64
    65func loadTemplates(templatesDir string) multitemplate.Render {
    66	r := multitemplate.New()
    67
    68	layouts, err := filepath.Glob(templatesDir + "layouts/*.tmpl")
    69	if err != nil {
    70		panic(err.Error())
    71	}
    72
    73	includes, err := filepath.Glob(templatesDir + "includes/*.tmpl")
    74	if err != nil {
    75		panic(err.Error())
    76	}
    77
    78	// Generate our templates map from our layouts/ and includes/ directories
    79	for _, layout := range layouts {
    80		files := append(includes, layout)
    81		r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...)))
    82	}
    83	return r
    84}
    85```
    86

View as plain text