templatesv1
import templatesv1 "pocketone.dev/ext/contracts/templates/v1"
Package templatesv1 is the templates/v1 contract: an extension's own pages as a set of Go html/template files that a project can override. An extension adds its set (usually go:embed) under a name; the provider renders a page from the set, taking the project's copy of any file it has, and ejects the set for the project to edit.
A set is every *.tpl file in an fs.FS, at any depth. Each file is one template named by its path without ".tpl" ("signin", "parts/head"), so pages include pieces with {{template "parts/head" .}}.
Source: ext/contracts/templates/v1.
Variables
var Contract = extkit.NewContract[Templates]("templates/v1", extkit.One)
Contract is templates/v1.
Functions
Add
func Add(app core.App, name string, files fs.FS)
Add registers an extension's template set on app under name (call it in Register). name is a plain folder name ("oauth"): the project's copy lives in a folder of that name.
Names
func Names(app core.App) []string
Names lists the sets added on app, sorted (for providers).
Parse
func Parse(base, overlay fs.FS) (*template.Template, error)
Parse builds a set's templates: every *.tpl in base, then every *.tpl in overlay (nil for none), which replaces the template of the same name. Providers and an extension rendering its own set without a provider both use it.
Set
func Set(app core.App, name string) (fs.FS, bool)
Set returns the files added under name (for providers).
Write
func Write(e *core.RequestEvent, t *template.Template, page string, status int, data any) error
Write executes page of t with data and writes it as the whole HTML response to e (status 0 = 200). Nothing is written if the template fails.
Types
ExistsError
type ExistsError struct{ Files []string }
ExistsError is Eject's refusal: the project's copies of these files differ from the built-in ones.
ExistsError.Error
func (e *ExistsError) Error() string
Templates
type Templates interface {
// Render writes page of set name as the whole response to e (status 0 = 200), with data as the template's dot.
// Each template comes from the project's copy when it has one, else from the set.
Render(e *core.RequestEvent, name, page string, status int, data any) error
// Eject copies set name's files into the project's copy, for the project to edit. Files the copy lacks are written
// and files identical to the built-in ones are left alone; files that differ (the project's edits) are only
// overwritten with force: otherwise it refuses with an *ExistsError naming them, writing nothing. It returns the
// files written.
Eject(name string, force bool) (written []string, err error)
}
Templates renders and ejects the sets added with Add.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.