render

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 14 Imported by: 14

Documentation

Overview

Package render implements template inheritance and exposes functions to render HTML.

inspired by http://elithrar.github.io/article/approximating-html-template-inheritance and https://github.com/sourcegraph/thesrc/blob/master/app/handler.go

It also exports two types Binary and HMTL. Both wrap a http.HandlerFunc-like function with an error return value and argument the response.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PlainError

func PlainError(w http.ResponseWriter, r *http.Request, statusCode int, err error)

PlainError helps rendering user errors

Types

type Binary

type Binary func(resp http.ResponseWriter, req *http.Request) error

Binary sets Content-Description and Content-Transfer-Encoding if h returns an error it returns http status 500

func (Binary) ServeHTTP

func (h Binary) ServeHTTP(resp http.ResponseWriter, req *http.Request)

type ErrorHandlerFunc added in v1.10.0

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, int, error)

ErrorHandlerFunc is just like http.HandlerFunc but with an additional status code and error

type FuncInjector added in v1.6.0

type FuncInjector func(*http.Request) interface{}

FuncInjector needs to return a function that is admissible for (html/template).FuncMap

type Option

type Option func(*Renderer) error

func AddTemplates

func AddTemplates(files ...string) Option

AddTemplates adds filenames for the next call to parseTempaltes

func BaseTemplates

func BaseTemplates(bases ...string) Option

func ErrorTemplate added in v1.8.0

func ErrorTemplate(name string) Option

ErrorTemplate sets the filename of template that should be used for errors.

func FuncMap

func FuncMap(m template.FuncMap) Option

func InjectTemplateFunc added in v1.6.0

func InjectTemplateFunc(name string, fn FuncInjector) Option

InjectTemplateFunc sets template functions, just like FuncMap. But in comparrison, these here are evaluated once with the request before being assigned to the template. This allows for accessing the requests cookies or headers, for instance.

func SetErrorHandler added in v1.10.0

func SetErrorHandler(fn ErrorHandlerFunc) Option

overwrites the default error handler and allows for inspecting errors before rendering them

func SetLogger

func SetLogger(l log.Logger) Option

type RenderFunc

type RenderFunc func(w http.ResponseWriter, req *http.Request) (interface{}, error)

type Renderer

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

func New

func New(fs http.FileSystem, opts ...Option) (*Renderer, error)

New creates a new Renderer

func (*Renderer) Error

func (r *Renderer) Error(w http.ResponseWriter, req *http.Request, status int, err error)

func (*Renderer) GetReloader

func (r *Renderer) GetReloader() func(http.Handler) http.Handler

func (*Renderer) HTML

func (r *Renderer) HTML(name string, f RenderFunc) http.HandlerFunc

func (*Renderer) Reload

func (r *Renderer) Reload() error

func (*Renderer) Render

func (r *Renderer) Render(w http.ResponseWriter, req *http.Request, name string, status int, data interface{}) error

func (*Renderer) StaticHTML

func (r *Renderer) StaticHTML(name string) http.Handler

Jump to

Keyboard shortcuts

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