https://github.com/codekoala/templ-component-opts
Generate code for templ component option structs.
https://github.com/codekoala/templ-component-opts
go gogenerate golang templ
Last synced: 5 months ago
JSON representation
Generate code for templ component option structs.
- Host: GitHub
- URL: https://github.com/codekoala/templ-component-opts
- Owner: codekoala
- License: mit
- Created: 2023-12-22T20:46:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-26T23:58:23.000Z (over 2 years ago)
- Last Synced: 2025-11-22T19:03:36.912Z (7 months ago)
- Topics: go, gogenerate, golang, templ
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Templ Component Opts
This project is designed to help generate code to simplify passing options to a [templ component](https://github.com/a-h/templ).
Rather than creating templ components that take multiple parameters, they can instead take a single parameters that includes the various bits of context that are important for the component.
For example:
```templ
// example/view/index.templ
package view
import (
"time"
"github.com/codekoala/templ-component-opts/example/view/component/book"
)
templ Index() {
@book.Book(book.With(
book.Title("1984"),
book.Author("George Orwell"),
book.Published(MustParse("June 8, 1949")),
))
@book.Book(book.With(
book.Display(false),
book.Title("Dune"),
book.Author("Frank Herbert"),
book.Published(MustParse("August 1, 1965")),
))
}
// MustParse parses a date or panics.
func MustParse(value string) time.Time {
val, err := time.Parse("January 2, 2006", value)
if err != nil {
panic(err)
}
return val
}
```
## Installation
You can install `templ-component-opts` using the `go install` command:
```sh
$ go install github.com/codekoala/templ-component-opts@latest
```
This will download and install the executable in your `$GOPATH/bin` directory.
## Usage
To use `templ-component-opts`, simply create a struct with the various options that you may want to pass to a templ component and include the `//templ:component-opts` directive:
```go
// example/view/component/book/book.go
package book
import "time"
// Opts defines options for the Book templ component.
//
//templ:component-opts
type Opts struct {
Title string
Author string
Published time.Time
Display bool `default:"true"`
}
```
As illustrated by teh `Display` field, default values can be specified using the `default` tag.
Run the `templ-component-opts` tool pointing to the project directory:
```sh
$ templ-component-opts .
```
This will produce a new file with the suffix `_tcogen.go`:
```go
// example/view/component/book/book_tcogen.go
// Code generated by templ-component-opts; DO NOT EDIT.
// This file contains functions and methods for use with Opts in templ components.
package book
import (
"strconv"
"time"
)
type Opt func(*Opts)
func DefaultOpts() *Opts {
out := &Opts{Display: true}
return out
}
func With(opts ...Opt) *Opts {
out := DefaultOpts()
out.With(opts...)
return out
}
func (o *Opts) With(opts ...Opt) *Opts {
for _, opt := range opts {
opt(o)
}
return o
}
func Title(in string) Opt {
return func(opts *Opts) {
opts.Title = in
}
}
func Author(in string) Opt {
return func(opts *Opts) {
opts.Author = in
}
}
func Published(in time.Time) Opt {
return func(opts *Opts) {
opts.Published = in
}
}
func Display(in bool) Opt {
return func(opts *Opts) {
opts.Display = in
}
}
func (o *Opts) DisplayStr() string {
return strconv.FormatBool(o.Display)
}
```
In the interest of keeping things as simple as possible, package level functions are created to populate fields in the annotated struct. For some data types, such as `int64`, `float64`, and `bool`, additional methods are generated on the struct to return a stringified version of the field.
## `go generate`
Add a single `//go:generate templ-component-opts .` in your project, and `go generate` should automatically produce the `_tcogen.go` files for any struct with the `//templ:component-opts` directive.
## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.