templates/v1
An extension that serves pages of its own (tokens' OAuth sign-in, consent and MFA screens) keeps them as a set of Go
html/template files, usually embedded in the binary. A project themes them by ejecting the set and editing its copy:
any file it has replaces the built-in one, and anything it leaves out falls back. The format is always Go templates,
so the built-in pages and the ejected ones are the same files.
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 shared pieces with {{template "parts/head" .}}.
An app service contract, one provider (templates.go).
Use
Add your set in Register, then render a page through the provider at request time, falling back to your own set
when nothing provides the contract:
//go:embed templates/oauth
var oauthFiles embed.FS
files, _ := fs.Sub(oauthFiles, "templates/oauth")
templatesv1.Add(app, "oauth", files)
// in a handler
if t, ok := templatesv1.Contract.Of(app); ok {
return t.Render(e, "oauth", "signin", http.StatusOK, data)
}
set, err := templatesv1.Parse(files, nil)
if err != nil {
return err
}
return templatesv1.Write(e, set, "signin", http.StatusOK, data)
Declare the contract in WorksWith. Pass anything that must not be escaped (ready-made HTML) as template.HTML.
Provide
Implement templatesv1.Templates (Render, Eject) and provide it in Register. Read the sets with
templatesv1.Set and templatesv1.Names, and build each page with templatesv1.Parse(set, projectCopy) and
templatesv1.Write, so every provider renders the same way:
templatesv1.Contract.Provide(app, myTemplates)
Extensions
| Extension | |
|---|---|
| templates | provides |
| api-tokens | works with |
License
MIT
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.