Documentation ¶
Index ¶
- Variables
- func NewContextFunc(m map[string]any) func(string) any
- type Error
- type FileReadFunc
- type Option
- func WithBaseDir(dir string) Option
- func WithContentType(contentType string) Option
- func WithDelims(open, close string) Option
- func WithFileFindFunc(fn func(filename string) string) Option
- func WithFileReadFunc(fn FileReadFunc) Option
- func WithFileReadOnRender(yes bool) Option
- func WithFunction(name string, fn any) Option
- func WithFunctions(fns template.FuncMap) Option
- func WithLogger(l *slog.Logger) Option
- func WithTemplateFromFiles(name string, files ...string) Option
- func WithTemplateFromStrings(name string, strings ...string) Option
- func WithTemplatesFromFiles(ts map[string][]string) Option
- func WithTemplatesFromStrings(ts map[string][]string) Option
- type Options
- type Templates
- func (t Templates) Render(name string, data any) (s string, err error)
- func (t Templates) RenderTemplate(name, templateName string, data any) (s string, err error)
- func (t Templates) Respond(w http.ResponseWriter, name string, data any)
- func (t Templates) RespondTemplate(w http.ResponseWriter, name, templateName string, data any)
- func (t Templates) RespondTemplateWithStatus(w http.ResponseWriter, name, templateName string, data any, status int)
- func (t Templates) RespondWithStatus(w http.ResponseWriter, name string, data any, status int)
Constants ¶
This section is empty.
Variables ¶
var ErrUnknownTemplate = fmt.Errorf("unknown template")
ErrUnknownTemplate will be returned by Render function if the template does not exist.
Functions ¶
Types ¶
type Error ¶
Error is a common error type that holds information about error message and template name.
type FileReadFunc ¶ added in v0.1.1
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 ¶
WithBaseDir sets the directory in which template files are stored.
func WithContentType ¶
WithContentType sets the content type HTTP header that will be written on Render and Response functions.
func WithDelims ¶
WithDelims sets the delimiters used in templates.
func WithFileFindFunc ¶
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
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 ¶
WithFunction adds a function to templates.
func WithFunctions ¶
WithFunctions adds function map to templates.
func WithLogger ¶ added in v0.8.0
WithLogger sets the function that will perform message logging. Default is slog.Default().
func WithTemplateFromFiles ¶
WithTemplateFromFiles adds a template parsed from files.
func WithTemplateFromStrings ¶
WithTemplateFromStrings adds a template parsed from string.
func WithTemplatesFromFiles ¶
WithTemplatesFromFiles adds a map of templates parsed from files.
func WithTemplatesFromStrings ¶
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 (Templates) RenderTemplate ¶
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 ¶
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.