Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

A type-safe HTML template rendering engine for Go.

## Table of Contents
...

A type-safe HTML template rendering engine for Go.


## Table of Contents

- [Problem Statement](#problem-statement)
Expand All @@ -24,6 +18,7 @@ A type-safe HTML template rendering engine for Go.
- [Usage Examples](#usage-examples)
- [Template Generation](#template-generation)
- [Configuration](#configuration)
- [Development Requirements](#development-requirements)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -61,7 +56,7 @@ type HomeData struct {
reg, _ := templator.NewRegistry[HomeData](fs)

// Get type-safe handler and execute template
home, _ := reg.GetHome()
home, _ := reg.Get("home")
home.Execute(ctx, w, HomeData{
Title: "Welcome",
Content: "Hello",
Expand All @@ -84,7 +79,13 @@ home.Execute(ctx, w, struct{
## Installation

```bash
go install github.com/alesr/templator
go get github.com/alesr/templator@latest
```

Install the code generator binary (optional):

```bash
go install github.com/alesr/templator/cmd/generate@latest
```

## Quick Start
Expand Down Expand Up @@ -117,7 +118,7 @@ func main() {
}

// Get type-safe handler for home template
home, err := reg.GetHome()
home, err := reg.Get("home")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -154,8 +155,8 @@ homeReg := templator.NewRegistry[HomeData](fs)
aboutReg := templator.NewRegistry[AboutData](fs)

// Get handlers
home, _ := homeReg.GetHome()
about, _ := aboutReg.GetAbout()
home, _ := homeReg.Get("home")
about, _ := aboutReg.Get("about")

// Type safety enforced at compile time
home.Execute(ctx, w, HomeData{...}) // ✅ Compiles
Expand Down Expand Up @@ -246,29 +247,42 @@ This validation happens when loading the template, not during execution, helping

## Template Generation

Templates are automatically discovered and type-safe methods are generated:
Generate accessors in your package:

```bash
go run github.com/alesr/templator/cmd/generate \
-package myapp \
-templates ./templates \
-out ./templator_accessors_gen.go
```

Then use the generated wrapper:

```go
reg, _ := templator.NewRegistry[HomeData](fs)
tpl := NewTemplateAccessors(reg)
home, _ := tpl.GetHome()
```

Template names are mapped to wrapper methods using title-cased path components:

```zsh
templates/
├── home.html -> reg.GetHome()
├── about.html -> reg.GetAbout()
├── home.html -> tpl.GetHome()
├── about.html -> tpl.GetAbout()
└── components/
└── header.html -> reg.GetComponentsHeader()

# Generate methods
go generate ./...
└── header.html -> tpl.GetComponentsHeader()
```

The generation process creates a `templator_methods.go` file containing type-safe method handlers for each template. For example:
This creates a generated file (for example `templator_accessors_gen.go`) with wrapper methods such as:

```go
// Code generated by go generate; DO NOT EDIT.
func (r *Registry[T]) GetHome() (*Handler[T], error) {
return r.Get("home")
func (r *TemplateAccessors[T]) GetHome() (*templator.Handler[T], error) {
return r.registry.Get("home")
}

func (r *Registry[T]) GetAbout() (*Handler[T], error) {
return r.Get("about")
func (r *TemplateAccessors[T]) GetAbout() (*templator.Handler[T], error) {
return r.registry.Get("about")
}
```

Expand All @@ -286,9 +300,9 @@ reg, err := templator.NewRegistry[HomeData](
)
```

### Development Requirements
## Development Requirements

- Go 1.21 or higher
- Go 1.24 or higher

## License

Expand Down
Loading