Skip to content

kapmahc/multitemplate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multitemplate

Build Status codecov Go Report Card GoDoc

This is a custom HTML render to support multi templates, ie. more than one *template.Template.

Usage

Start using it

Download and install it:

$ go get github.com/gin-contrib/multitemplate

Import it in your code:

import "github.com/gin-contrib/multitemplate"

Simple example

package main

import (
	"html/template"

	"github.com/gin-contrib/multitemplate"
	"gopkg.in/gin-gonic/gin.v1"
)

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
}

func main() {
	router := gin.Default()
	router.HTMLRender = createMyRender()
	router.GET("/", func(c *gin.Context) {
		c.HTML(200, "index", data)
	})
	router.Run(":8080")
}

Advanced example

Approximating html/template Inheritance

package main

import (
	"html/template"
	"path/filepath"

	"github.com/gin-contrib/multitemplate"
	"gopkg.in/gin-gonic/gin.v1"
)

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
}

About

This is a custom HTML render to support multi templates

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%