templateutil

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPathAliases

func AddPathAliases(tmpl *template.Template, names []string) error

func DefinedTemplates

func DefinedTemplates(name, src string) ([]string, error)

func FilterFuncMap

func FilterFuncMap(funcs template.FuncMap, required map[string]struct{}) template.FuncMap

func FunctionNameSignature

func FunctionNameSignature(funcs template.FuncMap) string

func FunctionNameSignatureFromNames

func FunctionNameSignatureFromNames(names []string) string

func FunctionNameSignatureFromSet

func FunctionNameSignatureFromSet(names map[string]struct{}) string

func HTMLAttrs added in v0.15.0

func HTMLAttrs(attrs map[string]string) template.HTMLAttr

HTMLAttrs writes attrs as HTML attributes, sorted by name, each after a space, with their values escaped.

func MergeFuncMaps

func MergeFuncMaps(funcMaps ...template.FuncMap) template.FuncMap

func MergeFunctionSignatures

func MergeFunctionSignatures(signatures ...string) string

func Names

func Names(funcs template.FuncMap) map[string]struct{}

func NormalizeContractType

func NormalizeContractType(typeName string) string

func PathAliases

func PathAliases(name string) []string

func PathBase

func PathBase(name string) string

func ReferencedTemplates

func ReferencedTemplates(name, src string) ([]string, error)

func RequiredFuncs

func RequiredFuncs(name, src string) ([]string, error)

func WriteAttr added in v0.15.0

func WriteAttr(b *strings.Builder, name, value string)

WriteAttr writes the attribute name="value" to b, after a space, with value escaped.

Types

type CachedTemplate

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

func NewCachedTemplate

func NewCachedTemplate(
	base *template.Template,
	requiredFuncs map[string]struct{},
	contracts map[string]RootContract,
) *CachedTemplate

NewCachedTemplate keeps a parsed template with the functions its templates call and their typed root declarations.

func (*CachedTemplate) NoteSize added in v0.15.0

func (cached *CachedTemplate) NoteSize(entry string, size int)

NoteSize remembers the size of the output of a render of entry, for SizeHint.

func (*CachedTemplate) Release added in v0.15.0

func (cached *CachedTemplate) Release(t *template.Template)

Release takes back a copy of the template that Template or TemplateResolving returned, for a later render.

func (*CachedTemplate) RequiredFuncs added in v0.15.0

func (cached *CachedTemplate) RequiredFuncs() map[string]struct{}

RequiredFuncs returns the names of the functions the template calls, or nil when they are not known. The map is shared; do not change it.

func (*CachedTemplate) RootContracts added in v0.15.0

func (cached *CachedTemplate) RootContracts() map[string]RootContract

RootContracts returns the typed root declarations of the template's files by root name. The map is shared; do not change it.

func (*CachedTemplate) SizeHint added in v0.15.0

func (cached *CachedTemplate) SizeHint(entry string) int

SizeHint returns the capacity to start the output of a render of entry ("" for the template itself) with: the size of its last output and an eighth more, for output that grows a little, at most 4 MB; 0 before the first render.

func (*CachedTemplate) Template

func (cached *CachedTemplate) Template(functions template.FuncMap) (*template.Template, error)

Template returns a copy of the template with functions, those of the functions the template calls. Give it back with Release when it has executed.

func (*CachedTemplate) TemplateResolving added in v0.14.0

func (cached *CachedTemplate) TemplateResolving(resolve func(name string) (any, bool)) (*template.Template, error)

TemplateResolving is Template with the functions the template calls looked up by resolve, rather than filtered from a map of every function.

type ContractDeclaration added in v0.14.0

type ContractDeclaration struct {
	Annotation string
	Name       string
	Type       string
}

ContractDeclaration is one typed root declaration, such as "@model Site github.com/acme/site.Site".

func Declarations added in v0.15.0

func Declarations(src string) ([]ContractDeclaration, string)

Declarations returns the typed root declarations of a template source and the type of its first @dot declaration, or "".

type FileScan added in v0.14.0

type FileScan struct {
	Defined    []string
	Referenced []string
	Required   []string
	Contracts  []ContractDeclaration
	// Dot is the type the file declares for its dot with @dot, or "".
	Dot string
	// ParseErr is set when the file does not parse; Defined, Referenced and
	// Required are empty then.
	ParseErr error
	// contains filtered or unexported fields
}

FileScan is what one template file declares and uses: the templates it defines and calls, the functions it calls, and its typed root declarations.

func ScanTemplate added in v0.14.0

func ScanTemplate(name, src string) *FileScan

ScanTemplate reads what the template source declares and uses, parsing it once.

type RootContract

type RootContract struct {
	Annotation string
	Type       string
}

type Scanner added in v0.14.0

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

Scanner reads template files from one file system. With a key, which identifies the file system in the process, each file is scanned once, and whether a file exists and what a pattern matches asked once, and kept in the store, for template trees whose files do not change.

func (Scanner) DefinedTemplates added in v0.14.0

func (s Scanner) DefinedTemplates(names []string) map[string]struct{}

DefinedTemplates returns the names the files can be called by: their paths, base names and aliases, and the templates they define.

func (Scanner) Exists added in v0.15.0

func (s Scanner) Exists(name string) (bool, error)

Exists reports whether name is a file. With a key, the answer is remembered, also when the file is missing; errors other than a missing file are returned, and asked again next time.

func (Scanner) File added in v0.14.0

func (s Scanner) File(name string) (*FileScan, error)

File returns the scan of the template file name.

func (Scanner) Glob added in v0.15.0

func (s Scanner) Glob(pattern string) ([]string, error)

Glob returns the files pattern matches, as fs.Glob, without the directories that match. With a key, the matches are remembered. The slice may be shared: do not change it.

func (Scanner) IsFile added in v0.14.0

func (s Scanner) IsFile(name string) bool

IsFile reports whether name is a file. With a key, the answer is remembered; see Exists.

func (Scanner) ReferencedTemplates added in v0.14.0

func (s Scanner) ReferencedTemplates(names []string) map[string]struct{}

ReferencedTemplates returns the templates the files call. Files that cannot be read or parsed are skipped; parsing them for rendering reports why.

func (Scanner) RequiredFuncs added in v0.14.0

func (s Scanner) RequiredFuncs(names []string) (map[string]struct{}, error)

RequiredFuncs returns the functions the files call.

func (Scanner) RootContracts added in v0.14.0

func (s Scanner) RootContracts(names []string) (map[string]RootContract, error)

RootContracts returns the typed root declarations of the files by root name. A name declared with two types is an error.

The map may be shared: do not change it.

type Store

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

Store keeps what the template cache of one partial tree learns: parsed templates, and of each file system the tree reads, the scans of its files, which files exist and what include patterns match. Every tree root has a store of its own, so two trees over the same directory, such as two applications in one test process, do not see each other's answers, such as a file that was missing when the other one looked.

func NewStore

func NewStore() *Store

NewStore returns an empty store.

func (*Store) Load

func (store *Store) Load(key string) (*CachedTemplate, bool)

func (*Store) Mutex

func (store *Store) Mutex(key string) *sync.Mutex

func (*Store) Scanner added in v0.14.0

func (store *Store) Scanner(fsys fs.FS, key string) Scanner

Scanner returns a scanner for fsys. An empty key, or a nil store, scans files on every call.

func (*Store) Store

func (store *Store) Store(key string, entry *CachedTemplate)

func (*Store) StoreText added in v0.15.0

func (store *Store) StoreText(key string, tmpl *texttemplate.Template)

StoreText keeps a parsed text template under key; it is cloned for each render.

func (*Store) Text added in v0.15.0

func (store *Store) Text(key string) (*texttemplate.Template, bool)

Text returns the text template stored under key.

Jump to

Keyboard shortcuts

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