Generator CLI reference
cmd/generate scans a directory for .html templates and generates a Go
file with typed accessor methods over a templator Registry. Conceptual
walkthrough and usage habits are in Generate typed
accessors; this page is the exact
interface.
Running it
Section titled “Running it”# without installing:go run github.com/alesr/templator/cmd/generate [flags]
# or install once, then:generate [flags]
# or wired into go:generate inside a source file://go:generate go run github.com/alesr/templator/cmd/generate -package main -templates ./templates -out ./templator_accessors_gen.goThe generator parses templates with the same library it documents access
to: it is a plain flag-based command with no config file and no network
access at run time.
| Flag | Default | Meaning |
|---|---|---|
-templates |
templates |
Directory to scan, walked recursively. Only files with the .html extension (templator.ExtensionHTML) produce a method; everything else is ignored. |
-out |
./templator_accessors_gen.go |
Output file for the generated code. |
-package |
main |
Package name written into the generated file. Must be non-empty (the run fails otherwise with requires non-empty -package). |
-templator-import |
github.com/alesr/templator |
Import path written into the generated file. Must be non-empty (requires non-empty -templator-import). Use it if you vendor or fork the library. |
Behavior
Section titled “Behavior”- Scanning: the directory is walked recursively, in lexical order, so the generated methods are deterministic and diffs stay stable across runs.
- Method naming: the template’s path relative to
-templates, minus the.htmlextension, is split into segments; each segment is title-cased with English casing rules (golang.org/x/text/cases) and the results are concatenated afterGet.components/header.html→GetComponentsHeader. - Template file names must be valid Go identifier parts. The generator
title-cases names but does not sanitize them. A file such as
user-card.htmlmakes the emitted source unformattable and the run fails:could not format generated code: expected '(', found '-'(exit status 1). Keep template file names to letters and digits. - Template names: the slash-separated path without the extension is the
registry name passed to
registry.Get— e.g.components/header. - Output: the emitted code is formatted with
go/format(gofmt) and written with file mode0644. The first line is// Code generated by go generate; DO NOT EDIT. - Success message: on success the command prints
Methods generated and formatted successfully.to stdout and exits 0. On failure it prints the error to stderr and exits with status 1.
Generated output contract
Section titled “Generated output contract”The exact file emitted for a templates/ directory containing home.html
and components/header.html (verified against cmd/generate):
// Code generated by go generate; DO NOT EDIT.package main
import "github.com/alesr/templator"
// TemplateAccessors provides typed helper accessors over templator Registry.type TemplateAccessors[T any] struct { registry *templator.Registry[T]}
// NewTemplateAccessors creates a TemplateAccessors wrapper for a templator Registry.func NewTemplateAccessors[T any](registry *templator.Registry[T]) *TemplateAccessors[T] { return &TemplateAccessors[T]{registry: registry}}
// GetComponentsHeader returns a handler for the components/header template.func (r *TemplateAccessors[T]) GetComponentsHeader() (*templator.Handler[T], error) { return r.registry.Get("components/header")}
// GetHome returns a handler for the home template.func (r *TemplateAccessors[T]) GetHome() (*templator.Handler[T], error) { return r.registry.Get("home")}The wrapper is generic: build it per registry with the data type tied to
each registry’s T.
Regenerate
Section titled “Regenerate”The file is a build artifact of your template inventory — regenerate whenever templates are added, renamed, moved, or deleted:
go generate ./...Committing the generated file keeps the build green for teammates and CI that do not run generation, at the cost of a merge conflict only when the same template is renamed twice.