templatereloader

package
v0.701.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 6 Imported by: 1

README

templatereloader

A templatereloader loads templates from an embedded file system in normal builds and supports live disk reloading in debug and race builds.

See the package documentation for its public API, AI.md for version-specific implementation guidance, and the jawsboot example for integration.

Documentation

Overview

Package templatereloader provides a build-aware github.com/linkdata/jaws.TemplateLookuper.

New parses templates from an io/fs.FS once in normal builds. Race and debug builds instead return a TemplateReloader that reparses the corresponding files from disk, so fpath and relpath must describe the same logical template set in both modes.

Reload failures retain the last successfully parsed templates. Use TemplateReloader.LastError and TemplateReloader.Path for diagnostics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(fsys fs.FS, fpath, relpath string) (jaws.TemplateLookuper, error)

New returns a jaws.TemplateLookuper for the templates matched by fpath.

In normal builds the templates are parsed once from fsys. When deadlock.Debug is set (race or debug builds) it instead returns a TemplateReloader that reparses from disk under relpath, so template edits take effect without a restart; fsys is then unused.

The debug-build concrete type is observable via a type assertion to *TemplateReloader, whose TemplateReloader.LastError and TemplateReloader.Path expose reload diagnostics; in normal builds the value is a plain *html/template.Template and there are no reload diagnostics to report.

Because the two builds resolve templates differently, fpath must be a valid pattern under both io/fs.Glob (used against fsys in normal builds) and path/filepath.Glob (used against the on-disk tree in debug builds), and relpath must point at the on-disk root that mirrors the embedded fsys layout so that path.Join(relpath, fpath) matches the same templates on disk.

Types

type TemplateReloader

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

TemplateReloader reparses templates from disk at most once per second and is safe for concurrent use.

func (*TemplateReloader) LastError added in v0.500.0

func (tr *TemplateReloader) LastError() (err error)

LastError returns the last reload parse error, or nil after a successful reload.

It is safe to call on a nil *TemplateReloader.

Example
package main

import (
	"fmt"
	"html/template"

	"github.com/linkdata/jaws/lib/templatereloader"
)

func main() {
	tmpl := template.Must(template.New("index.html").Parse("ok"))
	lookuper, ok := any(tmpl).(interface {
		Lookup(string) *template.Template
	})
	if !ok {
		panic("template does not implement Lookup")
	}
	fmt.Println(lookuper.Lookup("index.html") != nil)

	var reloader *templatereloader.TemplateReloader
	fmt.Println(reloader.LastError())

}
Output:
true
<nil>

func (*TemplateReloader) Lookup

func (tr *TemplateReloader) Lookup(name string) *template.Template

Lookup returns the named template, reparsing from disk when the reload interval has elapsed.

If reparsing fails, Lookup records the error for TemplateReloader.LastError and retains the last successful templates. It does not retry until another interval elapses.

The zero value returns nil.

func (*TemplateReloader) Path

func (tr *TemplateReloader) Path() (s string)

Path returns the glob pattern templates are reparsed from.

It is safe to call on a nil *TemplateReloader, in which case it returns "".

Jump to

Keyboard shortcuts

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