tmplcache

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tmplcache keeps texts that are no Go code in files of their own, the SQL of a store or a prompt, and hands them out filled in. The files are templates. A program reads and parses all of them once, when it starts, so a text that does not parse stops the start and not a request, and it looks them up by their path afterwards. The package knows nothing of this module: the caller says which files, and what each is filled in with.

Index

Constants

View Source
const Suffix = ".gotmpl"

Suffix is what the name of a template file ends with. Preload leaves every other file alone.

Variables

View Source
var (
	// ErrNoTemplates is returned by Preload for a file system that holds no template.
	ErrNoTemplates = errors.New("no template files found")
	// ErrNotFound is returned by Execute for a path that no template has.
	ErrNotFound = errors.New("template not found")
)

Functions

This section is empty.

Types

type Cache

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

Cache holds the parsed templates of one file system by their path in it. Preload writes it once and nothing changes it afterwards, so any number of goroutines may execute its templates at the same time.

func Preload

func Preload(fsys fs.FS, funcs template.FuncMap) (*Cache, error)

Preload reads every file of fsys whose name ends with Suffix, at any depth, and parses them together, each under its path: one template can include another with {{template "user/columns.gotmpl" .}}. funcs are the functions the templates may call; nil means none. A template that names a key its data does not have fails when it is executed, and so does one that includes a template that is not there.

func (*Cache) Execute

func (c *Cache) Execute(path string, data any) (string, error)

Execute fills in the template at path with data and returns the text. A text that stops halfway is not returned.

func (*Cache) Paths

func (c *Cache) Paths() []string

Paths lists the templates of the cache, sorted.

Jump to

Keyboard shortcuts

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