https://github.com/dennwc/reago
Naive experiment with Go HTML components based on Go templates.
https://github.com/dennwc/reago
Last synced: 3 months ago
JSON representation
Naive experiment with Go HTML components based on Go templates.
- Host: GitHub
- URL: https://github.com/dennwc/reago
- Owner: dennwc
- License: mit
- Created: 2023-05-31T08:01:05.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-31T08:01:30.000Z (about 2 years ago)
- Last Synced: 2025-03-10T19:13:10.202Z (3 months ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go HTML component engine
This is just a simple/naive experiment for making HTML components based on the Go templates.
Components are defined in `./component/.gohtml` and will replace `content` in the main HTML of the page.
## Simple component
For example, component can be defined like this:
```html
{{define "mybutton"}}{{ .Content }}
{{end}}
```And used like this:
```html
Hello!
```
The resulting HTML will be rendered as:
```html
Hello!
```
## Accessing data
Components can also access external data, which is exposed as `.DB` in the template context by the library user.
For example, when using the library, data interface can be defined as:
```go
type ExampleRecord struct {
ID int
Name string
}type ExampleDB struct {
}func (d *ExampleDB) Table() []ExampleRecord {
return []ExampleRecord{
{ID: 1, Name: "Foo"},
{ID: 2, Name: "Bar"},
}
}
```Then we can define a table component that shows this data:
```html
{{define "mytable"}}
{{range .DB.Table}}
{{.ID}}{{.Name}}
{{end}}
{{end}}
```And component can be used on the main HTML as:
```html
```
We can potentially let the user pass parameters to this component. For this we first need to add parameter to the method:
```go
func (d *ExampleDB) Table(limit int) []ExampleRecord
```And pass this parameter from component tag attributes:
```html
{{range .DB.Table .Attrs.limit}}
```Usage on the main html:
```html
```
## License
MIT