render

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2018 License: MIT Imports: 2 Imported by: 0

README

render

Package render provides interface to render multiple templates with Gin.

Example

import (
   "html/template"
   
   "github.com/gin-gonic/gin"
   "github.com/rvflash/safe/cmd/ui/router/render"
)


func toHTML(s string) template.HTML {
   return template.HTML(s)
}

type mr struct{}

func (r mr) PageName() string {
   return "test"
}

func (r mr) TmplFiles() []string {
   return []string{"templates/tpl.html"}
}

func (r mr) FuncMap() template.FuncMap {
   return template.FuncMap{
       "toHTML": toHTML,
   }
}

// ...

gs := gin.New()
rs := templates.New()
rs.Add(&mr{})
gs.HTMLRender = rs.HTMLRender()

Documentation

Overview

Package render provides interface to render multiple templates with Gin

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type R

type R struct {
	// contains filtered or unexported fields
}

R is the registry of renders.

func New

func New() *R

New returns an instance a new registry of template. It will index all HTML templates to render.

Example
package main

import (
	"html/template"

	"github.com/gin-gonic/gin"
	"github.com/rvflash/safe/cmd/ui/render"
)

type mr struct{}

func (r mr) PageName() string {
	return "test"
}

func (r mr) TmplFiles() []string {
	return []string{"testdata/tpl.html"}
}

func (r mr) FuncMap() template.FuncMap {
	return nil
}

func main() {
	gin.SetMode(gin.TestMode)
	gs := gin.New()
	rs := render.New()
	rs.Add(&mr{})
	gs.HTMLRender = rs.HTMLRender()
}

func (*R) Add

func (r *R) Add(h Render) *R

Add adds a render into the registry.

func (*R) HTMLRender

func (r *R) HTMLRender() multitemplate.Render

HTMLRender returns a HTML render as expected by Gin.

type Render

type Render interface {
	// PageName returns the name of the page.
	PageName() string
	// TmplFiles returns the paths to the template file(s) of this page.
	TmplFiles() []string
	// FuncMap returns the custom template functions to use.
	FuncMap() template.FuncMap
}

Render must be implemented by any handler to render multiple templates.

Jump to

Keyboard shortcuts

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