templatereloader

package
v0.600.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 6 Imported by: 1

README

templatereloader

A templatereloader is a jaws.TemplateLookuper that will reload templates from disk as needed if running with -tags debug or -race. If not, it simply calls template.New("").ParseFS(fsys, fpath) and returns the parsed *template.Template directly, adding no per-lookup indirection.

For example usage, see jawsboot/README.md

Documentation

Overview

Package templatereloader provides a jaws.TemplateLookuper that reparses templates from disk while running in debug or race builds.

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 reloads and reparses templates if more than one second has passed since the last reload.

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 the templates from disk first when more than one second has passed since the last reload.

If a reload fails to parse (for example while a template file is being edited), the last successfully parsed templates are retained and used, so a transient parse error does not take down a running server. Lookup never panics on a reload error.

A failed reload still advances the reload timestamp, so the next reparse is deferred until the interval elapses again; the last-good templates keep being served meanwhile, and a fix made within that window is not picked up until the window reopens.

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