dotprovider

command
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

dotprovider example

A custom dot provider (the repository pattern): a small Go type exposes hardcoded in-memory data to templates as {{.Shop}}. templates/index.html ranges over {{.Shop.Products}} and looks one up with {{.Shop.Product 2}}.

Run it:

mise run example-dotprovider

Then open http://localhost:9006/.

Writing a dot provider

Implement xtemplate.Provider (FieldName() string, Prototype() any, Value(http.ResponseWriter, *http.Request) (any, error)), then register a factory via xtemplate.WithProvider(func() xtemplate.Provider { return p }) passed to app.Main. Each Instance / Reload invokes the factory for a fresh provider value. FieldName is the dot field name (Shop here); Prototype is a typed zero for reflection; Value returns the value assigned to {{.Shop}} for each request. Init is optional (save the instance context there if request-time code needs it). See main.go.

Documentation

Overview

Custom dot provider example: the "repository pattern". A custom provider exposes hardcoded in-memory data to templates as {{.Shop}}, which can range over products and look one up by id.

To write a dot provider: implement xtemplate.Provider (FieldName, Prototype, Value), then register a factory with xtemplate.WithProvider passed to app.Main. FieldName is the dot field name; Prototype is a typed zero value for reflection; Value returns the value assigned to that field for each request. Init is optional (Initializer).

Jump to

Keyboard shortcuts

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