Templates
Version 0.0.2 · Owners: @benallfree
Some extensions serve pages of their own, such as the OAuth sign-in, consent and MFA screens the CLI and MCP agents
sign in through. Templates lets you restyle them: eject an extension's templates into your app's pb_templates
folder and edit them. Any file you keep there replaces the built-in one; delete it to go back.
Install · Configure · Use · Integrations · Example
Install
Templates is built into pocketone and on by default; there's nothing to install. In your own Go build:
extkit.Setup(app, []extkit.Extension{templates.New(templates.Config{})}, nil, nil)
Configure
Nothing to configure. Your copies live in pb_templates/ next to pb_data (in a Go build, templates.Config.Dir
moves it). pocketone deploy and pocketone dev sync it like pb_hooks.
Use
See the template sets your app's extensions offer, and what you've changed:
pocketone template list
Eject one to edit it (leave out the name to eject every set):
pocketone template eject oauth
That copies every file of the set into pb_templates/oauth/. Edit any of them; the next request uses your copy, no
restart. Keep only the files you change if you like: the rest come from the built-in set.
Ejecting again is safe: files you haven't changed are left alone and missing ones are written back. Files you did
change are listed and only overwritten if you answer yes; without a terminal to ask in it refuses, and --force
overwrites them without asking.
The files are Go templates (html/template): {{.AppName}} prints a value (HTML-escaped), {{if .Error}}…{{end}}
and {{range .Providers}}…{{end}} branch and loop, and {{template "head" .}} includes another file of the set
(head.tpl). A broken file shows its error instead of the page, so you see what to fix.
Commands and APIs
Generated from the pocketone binary and the contracts this extension provides.
Commands
What it adds to the pocketone binary (CLI reference):
| Command | |
|---|---|
pocketone template eject [set] |
Copies a template set (every set without a name) into pb_templates/<set>/ for you to edit |
pocketone template list |
Lists the template sets extensions offer, and what you've changed in pb_templates |
Integrations
| Extension | How they fit |
|---|---|
| Tokens | Its OAuth pages are the oauth set: sign-in, MFA, one-time code, consent, device code, done and error, with a shared head and foot. Without Templates it serves them built in. |
Building on Templates
An extension adds its own set through the templates/v1 contract: embed a
folder of .tpl files, add it under a name in Register (templatesv1.Add(app, "mine", files)), and render a page
with templatesv1.Contract.Of(app)'s Render. Declare templates/v1 in WorksWith and render the embedded set
yourself (templatesv1.Parse, templatesv1.Write) when nothing provides it.
Example
A PocketBase app with one inline set, hello, served at /hello. Run go run . template eject hello, edit
pb_templates/hello/page.tpl, and reload.
// Command example is a PocketBase app with templates/v1 and one template set, "hello", served at /hello. Run
// `go run . template eject hello`, edit pb_templates/hello/page.tpl, and reload /hello.
package main
import (
"log"
"net/http"
"testing/fstest"
"ganigeorgiev.com/pocketbase/pocketbase"
"ganigeorgiev.com/pocketbase/pocketbase/core"
templatesv1 "pocketone.dev/ext/contracts/templates/v1"
"pocketone.dev/ext/extkit"
"pocketone.dev/ext/templates"
)
func main() {
app := pocketbase.New()
if _, err := extkit.Setup(app, []extkit.Extension{templates.New(templates.Config{})}, nil, nil); err != nil {
log.Fatal(err)
}
// an extension would go:embed its set; this one is inline
templatesv1.Add(app, "hello", fstest.MapFS{"page.tpl": {Data: []byte(`<h1>Hello, {{.}}!</h1>`)}})
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
se.Router.GET("/hello", func(e *core.RequestEvent) error {
return templatesv1.Contract.Must(app).Render(e, "hello", "page", http.StatusOK, "world")
})
return se.Next()
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
License
MIT
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.