template

package
v0.0.0-...-a1948f5 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package template is a thin wrapper around the standard html/template and text/template packages that implements a convenient registry to load and cache templates on the fly concurrently.

It was created to assist the JSVM plugin HTML rendering, but could be used in other Go code.

Example:

registry := template.NewRegistry()

html1, err := registry.LoadFiles(
	// the files set wil be parsed only once and then cached
	"layout.html",
	"content.html",
).Render(map[string]any{"name": "John"})

html2, err := registry.LoadFiles(
	// reuse the already parsed and cached files set
	"layout.html",
	"content.html",
).Render(map[string]any{"name": "Jane"})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Registry

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

Registry defines a templates registry that is safe to be used by multiple goroutines.

Use the Registry.Load* methods to load templates into the registry.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates and initializes a new templates registry with some defaults (eg. global "raw" template function for unescaped HTML).

Use the Registry.Load* methods to load templates into the registry.

func (*Registry) AddFuncs

func (r *Registry) AddFuncs(funcs map[string]any) *Registry

AddFuncs registers new global template functions.

The key of each map entry is the function name that will be used in the templates. If a function with the map entry name already exists it will be replaced with the new one.

The value of each map entry is a function that must have either a single return value, or two return values of which the second has type error.

Example:

r.AddFuncs(map[string]any{
  "toUpper": func(str string) string {
      return strings.ToUppser(str)
  },
  ...
})

func (*Registry) LoadFS

func (r *Registry) LoadFS(fsys fs.FS, globPatterns ...string) *Renderer

LoadFS caches (if not already) the specified fs and globPatterns pair as single template and returns a ready to use Renderer instance.

There must be at least 1 file matching the provided globPattern(s) (note that most file names serves as glob patterns matching themselves).

func (*Registry) LoadFiles

func (r *Registry) LoadFiles(filenames ...string) *Renderer

LoadFiles caches (if not already) the specified filenames set as a single template and returns a ready to use Renderer instance.

There must be at least 1 filename specified.

func (*Registry) LoadString

func (r *Registry) LoadString(text string) *Renderer

LoadString caches (if not already) the specified inline string as a single template and returns a ready to use Renderer instance.

type Renderer

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

Renderer defines a single parsed template.

func (*Renderer) Render

func (r *Renderer) Render(data any) (string, error)

Render executes the template with the specified data as the dot object and returns the result as plain string.

Jump to

Keyboard shortcuts

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