An open API service indexing awesome lists of open source software.

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.

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.