GinHTMLRender

package
v0.0.0-...-ce6400d Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package GinHTMLRender provides some sugar for gin's template rendering

This work is based on gin contribs multitemplate render https://github.com/gin-gonic/contrib/blob/master/renders/multitemplate

Usage

router := gin.Default()

// Set html render options
htmlRender := GinHTMLRender.New()
htmlRender.Debug = gin.IsDebugging()
htmlRender.Layout = "layouts/default"
// htmlRender.TemplatesDir = "templates/" // default
// htmlRender.Ext = ".html"               // default

// Tell gin to use our html render
router.HTMLRender = htmlRender.Create()

Structure

|-- templates/
    |--
    |-- 400.html
    |-- 404.html
    |-- layouts/
        |--- default.html
    |-- articles/
        |--- list.html
        |--- form.html

And if you want to render `templates/articles/list.html` in your handler

c.HTML(http.StatusOK, "articles/list", "")

Index

Constants

View Source
const (
	// TemplatesDir holds the location of the templates
	TemplatesDir = "templates/"
	// Layout is the file name of the layout file
	Layout = "layout"
	// Ext is the file extension of the rendered templates
	Ext = ".html"
	// Debug enables debug mode
	Debug = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Render

type Render struct {
	Templates    map[string]*template.Template
	Files        map[string][]string
	TemplatesDir string
	Layout       string
	Ext          string
	Debug        bool
}

Render implements gin's HTMLRender and provides some sugar on top of it

func New

func New() Render

New returns a fresh instance of Render

func (*Render) Add

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

Add assigns the name to the template

func (*Render) AddFromFiles

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

AddFromFiles parses the files and returns the result

func (*Render) Create

func (r *Render) Create() *Render

Create goes through the `TemplatesDir` creating the template structure for rendering. Returns the Render instance.

func (*Render) Instance

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

Instance implements gin's HTML render interface

func (*Render) Validate

func (r *Render) Validate()

Validate checks if the directory and the layout files exist as expected and configured

Jump to

Keyboard shortcuts

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