multitemplate

package
v0.0.0-...-a0b1be5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2023 License: MIT, MIT Imports: 5 Imported by: 0

README

Multitemplate

Run Tests 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

See example/simple/example.go

package main

import (
  "github.com/gin-contrib/multitemplate"
  "github.com/gin-gonic/gin"
)

func createMyRender() multitemplate.Renderer {
  r := multitemplate.NewRenderer()
  r.AddFromFiles("index", "templates/base.html", "templates/index.html")
  r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html")
  return r
}

func main() {
  router := gin.Default()
  router.HTMLRender = createMyRender()
  router.GET("/", func(c *gin.Context) {
    c.HTML(200, "index", gin.H{
      "title": "Html5 Template Engine",
    })
  })
  router.GET("/article", func(c *gin.Context) {
    c.HTML(200, "article", gin.H{
      "title": "Html5 Article Engine",
    })
  })
  router.Run(":8080")
}
Advanced example

Approximating html/template Inheritance

See example/advanced/example.go

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 {
    layoutCopy := make([]string, len(layouts))
    copy(layoutCopy, layouts)
    files := append(layoutCopy, include)
    r.AddFromFiles(filepath.Base(include), files...)
  }
  return r
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DynamicRender

type DynamicRender map[string]*templateBuilder

DynamicRender type

func NewDynamic

func NewDynamic() DynamicRender

NewDynamic is the constructor for Dynamic templates

func (DynamicRender) Add

func (r DynamicRender) Add(name string, tmpl *template.Template)

Add new template

func (DynamicRender) AddFromFiles

func (r DynamicRender) AddFromFiles(name string, files ...string) *template.Template

AddFromFiles supply add template from files

func (DynamicRender) AddFromFilesFuncs

func (r DynamicRender) AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template

AddFromFilesFuncs supply add template from file callback func

func (DynamicRender) AddFromGlob

func (r DynamicRender) AddFromGlob(name, glob string) *template.Template

AddFromGlob supply add template from global path

func (DynamicRender) AddFromString

func (r DynamicRender) AddFromString(name, templateString string) *template.Template

AddFromString supply add template from strings

func (DynamicRender) AddFromStringsFuncs

func (r DynamicRender) AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template

AddFromStringsFuncs supply add template from strings

func (DynamicRender) Instance

func (r DynamicRender) Instance(name string, data interface{}) render.Render

Instance supply render string

type Render

type Render map[string]*template.Template

Render type

func New

func New() Render

New instance

func (Render) Add

func (r Render) Add(name string, tmpl *template.Template)

Add new template

func (Render) AddFromFiles

func (r Render) AddFromFiles(name string, files ...string) *template.Template

AddFromFiles supply add template from files

func (Render) AddFromFilesFuncs

func (r Render) AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template

AddFromFilesFuncs supply add template from file callback func

func (Render) AddFromGlob

func (r Render) AddFromGlob(name, glob string) *template.Template

AddFromGlob supply add template from global path

func (Render) AddFromString

func (r Render) AddFromString(name, templateString string) *template.Template

AddFromString supply add template from strings

func (Render) AddFromStringsFuncs

func (r Render) AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template

AddFromStringsFuncs supply add template from strings

func (Render) Instance

func (r Render) Instance(name string, data interface{}) render.Render

Instance supply render string

type Renderer

type Renderer interface {
	render.HTMLRender
	Add(name string, tmpl *template.Template)
	AddFromFiles(name string, files ...string) *template.Template
	AddFromGlob(name, glob string) *template.Template
	AddFromString(name, templateString string) *template.Template
	AddFromStringsFuncs(name string, funcMap template.FuncMap, templateStrings ...string) *template.Template
	AddFromFilesFuncs(name string, funcMap template.FuncMap, files ...string) *template.Template
}

Renderer type is the Agnostic Renderer for multitemplates. When gin is in debug mode then all multitemplates works with hot reloading allowing you modify file templates and seeing changes instantly. Renderer should be created using multitemplate.NewRenderer() constructor.

func NewRenderer

func NewRenderer() Renderer

NewRenderer allows create an agnostic multitemplate renderer depending on enabled gin mode

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL