templates

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: BSD-3-Clause Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownTemplate = fmt.Errorf("unknown template")

ErrUnknownTemplate will be returned by Render function if the template does not exist.

Functions

func NewContextFunc

func NewContextFunc(m map[string]any) func(string) any

NewContextFunc creates a new function that can be used to store and access arbitrary data by keys.

Types

type Error

type Error struct {
	Err      error
	Template string
}

Error is a common error type that holds information about error message and template name.

func (*Error) Error

func (e *Error) Error() string

type FileReadFunc added in v0.1.1

type FileReadFunc func(filename string) ([]byte, error)

FileReadFunc returns the content of file referenced by filename. It hes the same signature as os.ReadFile function.

type Option

type Option func(*Options)

Option sets parameters used in New function.

func WithBaseDir

func WithBaseDir(dir string) Option

WithBaseDir sets the directory in which template files are stored.

func WithContentType

func WithContentType(contentType string) Option

WithContentType sets the content type HTTP header that will be written on Render and Response functions.

func WithDelims

func WithDelims(open, close string) Option

WithDelims sets the delimiters used in templates.

func WithFileFindFunc

func WithFileFindFunc(fn func(filename string) string) Option

WithFileFindFunc sets the function that will return the file path on disk based on filename provided from files defind using WithTemplateFromFile or WithTemplateFromFiles.

func WithFileReadFunc added in v0.1.1

func WithFileReadFunc(fn FileReadFunc) Option

WithFileReadFunc sets the function that will return the content of template given the filename.

func WithFileReadOnRender added in v0.3.2

func WithFileReadOnRender(yes bool) Option

WithFileReadOnRender forces template files to be read and parsed every time Render or Respond functions are called. This is useful for quickly reloading template files, but with a performance cost. This functionality is disabled by default.

func WithFunction

func WithFunction(name string, fn any) Option

WithFunction adds a function to templates.

func WithFunctions

func WithFunctions(fns template.FuncMap) Option

WithFunctions adds function map to templates.

func WithLogger added in v0.8.0

func WithLogger(l *slog.Logger) Option

WithLogger sets the function that will perform message logging. Default is slog.Default().

func WithTemplateFromFiles

func WithTemplateFromFiles(name string, files ...string) Option

WithTemplateFromFiles adds a template parsed from files.

func WithTemplateFromStrings

func WithTemplateFromStrings(name string, strings ...string) Option

WithTemplateFromStrings adds a template parsed from string.

func WithTemplatesFromFiles

func WithTemplatesFromFiles(ts map[string][]string) Option

WithTemplatesFromFiles adds a map of templates parsed from files.

func WithTemplatesFromStrings

func WithTemplatesFromStrings(ts map[string][]string) Option

WithTemplatesFromStrings adds a map of templates parsed from strings.

type Options

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

Options holds parameters for creating Templates.

type Templates

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

Templates structure holds parsed templates.

func New

func New(opts ...Option) (t *Templates, err error)

New creates a new instance of Templates and parses provided files and strings.

func (Templates) Render

func (t Templates) Render(name string, data any) (s string, err error)

Render executes a template and returns the string.

func (Templates) RenderTemplate

func (t Templates) RenderTemplate(name, templateName string, data any) (s string, err error)

RenderTemplate executes a named template and returns the string.

func (Templates) Respond

func (t Templates) Respond(w http.ResponseWriter, name string, data any)

Respond executes template with provided data into buffer, then writes the the body to the response writer. A panic will be raised if the template does not exist or fails to execute.

func (Templates) RespondTemplate

func (t Templates) RespondTemplate(w http.ResponseWriter, name, templateName string, data any)

RespondTemplate executes a named template with provided data into buffer, then writes the the body to the response writer. A panic will be raised if the template does not exist or fails to execute.

func (Templates) RespondTemplateWithStatus

func (t Templates) RespondTemplateWithStatus(w http.ResponseWriter, name, templateName string, data any, status int)

RespondTemplateWithStatus executes a named template with provided data into buffer, then writes the the status and body to the response writer. A panic will be raised if the template does not exist or fails to execute.

func (Templates) RespondWithStatus

func (t Templates) RespondWithStatus(w http.ResponseWriter, name string, data any, status int)

RespondWithStatus executes a template with provided data into buffer, then writes the the status and body to the response writer. A panic will be raised if the template does not exist or fails to execute.

Jump to

Keyboard shortcuts

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