renders

package module
v0.0.0-...-0825a3f Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: BSD-3-Clause Imports: 16 Imported by: 3

README

Renders

Middleware renders is a go template render middlewaer for Tango.

Version

v0.2.0510 Added RenderBytes for Renderer and simplifed codes.

Installation

go get gitea.com/tango/renders

Simple Example

type RenderAction struct {
    renders.Renderer
}

func (x *RenderAction) Get() {
    x.Render("test.html", renders.T{
        "test": "test",
    })
}

func main() {
    t := tango.Classic()
    t.Use(renders.New(renders.Options{
        Reload: true, // if reload when template is changed
        Directory: "./templates", // Directory to load templates
        Funcs: template.FuncMap{
            "test": func() string {
                    return "test"
            },
        },
        // Vars is a data map for global
        Vars: renders.T{
            "var": var,
        }
        Charset: "UTF-8", // Appends the given charset to the Content-Type header. Default is UTF-8
        // Allows changing of output to XHTML instead of HTML. Default is "text/html"
        HTMLContentType: "text/html",
        DelimsLeft:"{{",
        DelimsRight:"}}", // default Delims is {{}}, if it conflicts with your javascript template such as angluar, you can change it.
    }))
}

License

This project is under BSD License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	ContentType   = "Content-Type"
	ContentLength = "Content-Length"
	ContentHTML   = "text/html"
	ContentXHTML  = "application/xhtml+xml"
)

Variables

This section is empty.

Functions

func Add

func Add(left, right int) int

func List

func List(l *list.List) chan interface{}

func Load

func Load(opt Options) (map[string]*template.Template, error)

Load prepares and parses all templates from the passed basePath

func LoadWithFuncMap

func LoadWithFuncMap(opt Options) (map[string]*template.Template, error)

LoadWithFuncMap prepares and parses all templates from the passed basePath and injects a custom template.FuncMap into each template

func Range

func Range(l int) []struct{}

func RangeN

func RangeN(start, last int) map[int]struct{}

func Sub

func Sub(left, right int) int

func Version

func Version() string

Version return the middleware's version

Types

type After

type After interface {
	AfterRender(string)
}

type AfterBuf

type AfterBuf interface {
	AfterRender(string, io.Reader)
}

type Before

type Before interface {
	BeforeRender(string)
}

type IRenderer

type IRenderer interface {
	SetRenderer(*Renders, *tango.Context, func(string), func(string), func(string, io.Reader))
}

type Options

type Options struct {
	// if reload templates
	Reload bool
	// Directory to load templates. Default is "templates"
	Directory string
	// Extensions to parse template files from. Defaults to [".tmpl"]
	Extensions []string
	// Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to [].
	Funcs template.FuncMap
	// Vars is a data map for global
	Vars T
	// Appends the given charset to the Content-Type header. Default is "UTF-8".
	Charset string
	// Allows changing of output to XHTML instead of HTML. Default is "text/html"
	HTMLContentType string
	// default Delims
	DelimsLeft, DelimsRight string
	// where the file stored
	FileSystem http.FileSystem
}

Options is a struct for specifying configuration options for the render.Renderer middleware

func (Options) IsExtMatch

func (o Options) IsExtMatch(fileName string) bool

IsExtMatch is a file name match the ext

type Renderer

type Renderer struct {
	Charset         string
	HTMLContentType string
	// contains filtered or unexported fields
}

func (*Renderer) Render

func (r *Renderer) Render(name string, bindings ...interface{}) error

Render a template

r.Render("index.html")
r.Render("index.html", renders.T{
           "name": value,
      })

func (*Renderer) RenderBytes

func (r *Renderer) RenderBytes(name string, binding ...interface{}) ([]byte, error)

RenderBytes Will not called before & after method.

func (*Renderer) SetRenderer

func (r *Renderer) SetRenderer(renders *Renders, ctx *tango.Context,
	before, after func(string), afterBuf func(string, io.Reader))

func (*Renderer) StatusRender

func (r *Renderer) StatusRender(status int, name string, bindings ...interface{}) error

func (*Renderer) Template

func (r *Renderer) Template(name string) *template.Template

type Renders

type Renders struct {
	Options
	// contains filtered or unexported fields
}

func New

func New(options ...Options) *Renders

func (*Renders) Handle

func (r *Renders) Handle(ctx *tango.Context)

func (*Renders) Render

func (r *Renders) Render(w io.Writer, name string, bindings ...interface{}) error

func (*Renders) RenderBytes

func (r *Renders) RenderBytes(name string, bindings ...interface{}) ([]byte, error)

type T

type T map[string]interface{}

Provides a common buffer to execute templates.

func (T) Merge

func (t T) Merge(at T) T

Jump to

Keyboard shortcuts

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